How to parse JSON using Node.js?
You can simply use JSON.parse
.
The definition of the JSON
object is part of the ECMAScript 5 specification. node.js is built on Google Chrome's V8 engine, which adheres to ECMA standard. Therefore, node.js also has a global object JSON
[docs].
Note - JSON.parse
can tie up the current thread because it is a synchronous method. So if you are planning to parse big JSON objects use a streaming json parser.
How to parse JSON in node js
User is already a list of objects, so you don't need to parse it. However, it is an array. So if you meant for it to be an array, you'd need to access the password by using this code:
console.log(user[0].password);
Parse json data seperated by new line in Nodejs
If I understand correctly, the data is not a valid JSON string, but rather JSON strings separated by line breaks. So this is how the data
can be converted into the array of objects with sorted key order:
'use strict';
const data =`{"response": "True", "controllerId": "2751", "level": "INFO", "logType": "watchdogResponse", "deviceId": "2751", "container": "firmware", "epoch": "1549996256", "module": "hostServices", "ts": "2019-02-13 00:00:56"}{"ts": "2019-02-13 00:01:10", "epoch": "1549996270", "level": "INFO", "module": "jr", "logType": "running", "controllerId": "2751", "deviceId": "2751", "rid": "EXTERNAL CALL", "val": "1549996260000"}{"ts": "2019-02-13 00:01:10", "epoch": "1549996270", "level": "INFO", "module": "applicationMain", "logType": "wifi", "controllerId": "2751", "deviceId": "2751", "networkStrength": "100.0"}`;
const sortedArrayOfObjects = data .split('\n') .map(JSON.parse) .map(obj => Object.entries(obj).sort().reduce( (o, [k, v]) => (o[k] = v, o), {}) );
console.log(sortedArrayOfObjects);
Parse json response in nodejs
Please try this:
var data=response[0].credentials;
for (var i=0; i<data.length; i++){
console.log(data[i]._id);
console.log(data[i].username);
}
Here is jsfiddle
Using Node.JS, how do I read a JSON file into (server) memory?
Sync:
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file', 'utf8'));
Async:
var fs = require('fs');
var obj;
fs.readFile('file', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
});
How to parse JSON in node js
User is already a list of objects, so you don't need to parse it. However, it is an array. So if you meant for it to be an array, you'd need to access the password by using this code:
console.log(user[0].password);
How to parse JSON with escaped doublequotes, using node.js?
Since that part of the data is JSON-within-JSON, you'd parse the JSON, then parse the JSON on the attributes
property:
const obj = JSON.parse(json);
obj.attributes = JSON.parse(obj.attributes);
Live Example:
const json = document.getElementById("json").textContent;
const obj = JSON.parse(json);
obj.attributes = JSON.parse(obj.attributes);
console.log(obj);
<pre id="json">{
"name": "John",
"attributes": "{\"key\":\"value\"}"
}</pre>
NodeJs JSON parsing issue
don't use JSON.stringify
as it is further changing the string representation of JSON object. An example of what is happening is below
Imagine if you have a data in your file as shown below
{
"key": "value"
}
When you read the file (using readFileSync
) and apply JSON.stringify
, it is converted to a new string as shown below. You can notice that the double quotes are now escaped
"{\"key\": \"value\"}"
Now when you will parse it using JSON.parse
then instead of getting the desired object
, you are going to get back the same string you read from the file.
You are basically first performing and then undoing the stringify operation
Related Topics
How to Escape Regular Expression Special Characters Using JavaScript
How to Refresh a Page Using JavaScript
Jquery Load More Data on Scroll
How to Add External Js Scripts to Vuejs Components
How to Upload a File with the Js Fetch API
Promise.All: Order of Resolved Values
JavaScript - Arrow Functions This in Event Handler
What Does "This" Mean in Jquery
Call a JavaScript Function at a Specific Time of Day
How to Listen to a "Style Change" Event
How to Add New Array Elements at the Beginning of an Array in JavaScript
How to Check a Radio Button with Jquery
When to Use Setattribute VS .Attribute= in JavaScript
Define Global Variable with Webpack
What's the Difference Between Window.Location and Document.Location in JavaScript