when reading logs, JSON changes from double quotes to single quotes

json, logging, node.js, parsing, streaming

Solution

`console.log` doesn't return JSON. It returns human-readable representation of this object.

So no, it's not JSON, it's something closer to JSON5.

If you want JSON to be displayed, you have to pass a string to console.log, i.e. `console.log(JSON.stringify(shouldThisBeValidJSON))`

Problem

With the apache logs (below) I'm able to parse out a JSON: ``` [2014.02.14_21.24.22.543] other info I don't care about json: { "petstore": "store_number_8", "dogs":{ "terrier":{ "total":2 } }, "cat":{ "siamese":{ "total":5 } } } ``` 1) Is this valid JSON? 2) Why would the double quotes be turning into single quotes? After I read it in, parse out the JSON, and display it I get the following: ``` { 'petstore': 'store_number_8', 'dogs':{ 'terrier':{ 'total':2 } }, 'cat':{ 'siamese':{ 'total':5 } } } ``` Btw, I'm using Node.js' `fs.createStream` to read in the logs, then simply doing a console out (so far I'm not doing any sanitization and eventually I will be writing it to a file). ``` fs.creatReadStream(logs).pipe(split()).on(data, function(line){ if(line.match(/json\:/)){ shouldThisBeValidJSON = JSON.parse(line.slice(line.indexOf('{'), line.length)); console.log(shouldThisBeValidJSON); } ``` Thank you in advance.

Original source