Json.parse return


#1

hi, i’m trying to conver json string to an object before insert to mongodb on final step, meteor return parse error from json i dont understand why, anyone can help me ?

console.log(contentpre);

{"id":1928, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1927, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1925, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1926, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1924, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1921, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}
{"id":1923, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}

I got this json, when i try to use json parse before collection insert meteor return

Meteor method import json result from phantomjs

Meteor.methods({
  runTestsellorderhadronium: function(options){
    command = spawn(phantomjs.path, ['assets/app/phantomDriversellorder.js']);
    command.stdout.on('data',  function (data) {
	var contentpre = String(data);
	console.log(contentpre);//works fine
	var substringcontent = contentpre.substring(0,6);
	if(substringcontent === '{"id":'){
		var myobj = JSON.parse(contentpre, function(k, v){});
		console.log(myobj);//dont work		
	}
    });
    command.stderr.on('data', function (data) {
      console.log('stderr sellorderhadronium: ' + data);
    });
    command.on('exit', function (code) {
      console.log('sellorderhadronium child process exited with code ' + code);
    });
  }
});

Meteor return an error when parsing json result from phantomjs

=> Exited with code: 8 W20160422-22:40:05.616(2)? (STDERR) W20160422-22:40:05.619(2)? (STDERR) undefined:2 W20160422-22:40:05.619(2)? (STDERR) {"id":1927, "auteur":"user1", "montant":5000, "prixunite":50000, "t W20160422-22:40:05.619(2)? (STDERR) ^ W20160422-22:40:05.619(2)? (STDERR) SyntaxError: Unexpected token { W20160422-22:40:05.620(2)? (STDERR) at Object.parse (native) W20160422-22:40:05.620(2)? (STDERR) at Socket.<anonymous> (server/server.js:28:20) W20160422-22:40:05.620(2)? (STDERR) at Socket.emit (events.js:95:17) W20160422-22:40:05.620(2)? (STDERR) at Socket.<anonymous> (_stream_readable.js:765:14) W20160422-22:40:05.620(2)? (STDERR) at Socket.emit (events.js:92:17) W20160422-22:40:05.621(2)? (STDERR) at emitReadable_ (_stream_readable.js:427:10) W20160422-22:40:05.621(2)? (STDERR) at emitReadable (_stream_readable.js:423:5) W20160422-22:40:05.621(2)? (STDERR) at readableAddChunk (_stream_readable.js:166:9) W20160422-22:40:05.622(2)? (STDERR) at Socket.Readable.push (_stream_readable.js:128:10) W20160422-22:40:05.622(2)? (STDERR) at Pipe.onread (net.js:529:21)


#2

maybe i should export to json file and import it ? :hushed:


#3

This is not valid JSON:

{ ... }
{ ... }
{ ... }

It’s three objects contatenated together. If you were to run JSON.parse on this, what would you expect to get back? An array of objects? In that case, your JSON should look like this:

[
{ ... },
{ ... },
{ ... }
]

JSON.parse is throwing the "Unexpected token {" error, because it doesn’t know how to handle another object concatentated after the first.


#4

tried to change into array, same return :hushed:

`

[{“id”:1935, “auteur”:“user1”, “montant”:5000, “prixunite”:50000, “total”: 250000}]
[{“id”:1936, “auteur”:“user1”, “montant”:5000, “prixunite”:50000, “total”: 250000}]

`

Return

`
W20160423-00:32:00.133(2)? (STDERR) undefined:2
W20160423-00:32:00.133(2)? (STDERR) [{“id”:1928, “auteur”:“user1”, “montant”:5000, “prixunite”:50000, "
W20160423-00:32:00.135(2)? (STDERR) ^
W20160423-00:32:00.172(2)? (STDERR) SyntaxError: Unexpected token [
W20160423-00:32:00.173(2)? (STDERR) at Object.parse (native)
W20160423-00:32:00.173(2)? (STDERR) at Socket. (server/server.js:28:20)
W20160423-00:32:00.173(2)? (STDERR) at Socket.emit (events.js:95:17)
W20160423-00:32:00.174(2)? (STDERR) at Socket. (stream_readable.js:765:14)
W20160423-00:32:00.175(2)? (STDERR) at Socket.emit (events.js:92:17)
W20160423-00:32:00.175(2)? (STDERR) at emitReadable
(_stream_readable.js:427:10)
W20160423-00:32:00.175(2)? (STDERR) at emitReadable (_stream_readable.js:423:5)
W20160423-00:32:00.176(2)? (STDERR) at readableAddChunk (_stream_readable.js:166:9)
=> Exited with code: 8
W20160423-00:32:00.177(2)? (STDERR) at Socket.Readable.push (_stream_readable.js:128:10)
W20160423-00:32:00.177(2)? (STDERR) at Pipe.onread (net.js:529:21)

`


#5

Same issue. This isn’t valid JSON:

[{"id":1935, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}]
[{"id":1936, "auteur":"user1", "montant":5000, "prixunite":50000, "total": 250000}]

What would you expect JSON.parse to return?


#6

trying to format string from phantomjs to mongodb collection :s