Node.js Parse JSON - Tutorial Kart
Node.js Parse JSON
Node.js Parse JSON
Node.js Parse JSON ¨C For parsing JSON data in Node.js, we can useJSON.parse() function of JavaScript
Engine.
Little information to use JSON Data
key:value is the building block.
{ } contains an element.
[ ] contains an array of elements.
An element can have multiplekey:value pairs.
Value can be a simple value like number or string etc., or an element or an array.
Elements in an Array could be accessed using index
Multiplekey:value pairs or elements are separated by comma
A Simple Example Node.js JSON Parsing Program
Following example helps you to use JSON.parse() function and access the elements from JSON Object.
nodejs-parse-json.js
// json data
var jsonData =
'{"persons":
[{"name":"John","city":"
New
York"},
// json
data
{"name":"Phil","city":"O
var jsonData = '{"persons":[{"name":"John","city":"New York"},{"name":"Phil","city":"Ohio"}]}';
hio"}]}';
// parse json
// parse
json =
var
jsonParsed
JSON.parse(jsonData);
var jsonParsed = JSON.parse(jsonData);
// access elements
console.log(jsonParsed.
// access elements
persons[0].name);
console.log(jsonParsed.persons[0].name);
Terminal Output for running nodejs-parse-json.js
arjun@arjunVPCEH26EN:~/works
pace/nodejs$ node
nodejs-parse-json.js
John
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node nodejs-parse-json.js
John
Example ¨C Node.js Parse JSON File
We shall read a File containing JSON data to a variable and parse that data.
Consider following JSON File, sample.json
sample.json
{
"persons": [{
"name": "John",
"city": "Kochi",
{ "phone": {
"office": "040-528"persons": [{
1258",
"home":"name": "John",
"9952685471"
"city": "Kochi",
}
"phone": {
},
{
"office": "040-528-1258",
"name": "Phil",
"home": "9952685471"
"city": "Varkazha",
"phone":} {
"office": "040-5288569",
"home":
},
"7955555472"
} {
}
"name": "Phil",
]
}
"city": "Varkazha",
"phone": {
"office": "040-528-8569",
"home": "7955555472"
}
}
]
}
Node.js JSON File Parsing Program
nodejs-parse-json-file.js
// include file system
module
var fs = require('fs');
// read file sample.json
file
fs.readFile('sample.json
',
// callback function
that is called when
reading file is done
function(err, data) {
// json data
var jsonData = data;
// parse json
var jsonParsed =
JSON.parse(jsonData);
// access elements
console.log(jsonParsed.
persons[0].name + "'s
office phone number is
"+
// include file system module
var fs = require('fs');
// read file sample.json file
fs.readFile('sample.json',
// callback function that is called when reading file is done
function(err, data) {
// json data
var jsonData = data;
// parse json
var jsonParsed = JSON.parse(jsonData);
// access elements
console.log(jsonParsed.persons[0].name + "'s office phone number is " + jsonParsed.persons[0].phone.office);
console.log(jsonParsed.persons[1].name + " is from " + jsonParsed.persons[0].city);
});
Run the above Node.js program.
Terminal output on running nodejs-parse-json-file.js
arjun@arjunVPCEH26EN:~/works
pace/nodejs$ node
nodejs-parse-jsonfile.js
arjun@arjun-VPCEH26EN:~/workspace/nodejs$
node nodejs-parse-json-file.js
John's
office phone
number
is
040-528John's office phone number is 040-528-1258
1258
Philis isfrom
from
Kochi
Phil
Kochi
Conclusion :
In this Node.js Tutorial ¨C Node.js JSON File Parsing ¨C we have learnt to parse JSON data from a variable or file
using JSON.parse() function with the help of example Node.js programs.
Node.js
?
Node.js Tutorial
Get Started W ith Node.js
?
Install Node.js Ubuntu Linux
?
Install Node.js Windows
?
Node.js - Basic Example
?
Node.js - Command Line Arguments
?
Node.js - Modules
?
Node.js - Modules
?
Node.js - Create a module
?
Node.js - Add new functions to Module
?
Node.js - Override functions of Module
?
Node.js - Callback Function
?
Node.js - forEach
Express.js
?
Express.js Tutorial
?
What is Express.js?
?
Express.js Application Example
?
Install Express.js
?
Express.js Routes
?
Express.js Middleware
?
Express.js Router
Node.js Buffers
?
Node.js Buffer - Create, Write, Read
?
Node.js Buffer - Length
?
Node.js - Convert JSON to Buffer
?
Node.js - Array to Buffer
Node.js HTTP
?
Node.js - Create HTTP Web Server
?
Node.js - Redirect URL
Node.js MySQL
?
Node.js MySQL
?
Node.js MySQL - Connect to MySQL Database
?
Node.js MySQL - SELECT FROM
?
Node.js MySQL - SELECT WHERE
?
Node.js MySQL - ORDER BY
?
Node.js MySQL - INSERT INTO
?
Node.js MySQL - UPDATE
?
Node.js MySQL - DELETE
?
Node.js MySQL - Result Object
Node.js MongoDB
?
Node.js MongoDB
?
Node.js - Connect to MongoDB
?
Node.js - Create Database in MongoDB
?
Node.js - Drop Database in MongoDB
?
Node.js - Create Collection in MongoDB
?
Node.js - Delete Collection in MongoDB
?
Node.js - Insert Documents to MongoDB Collection
?
MongoError: failed to connect to server
Node.js Mongoose
?
Node.js Mongoose Tutorial
?
Node.js Mongoose - Installation
?
Node.js Mongoose - Connect to MongoDB
?
Node.js Mongoose - Define a Model
?
Node.js Mongoose - Insert Single Document to MongoDB
?
Node.js Mongoose - Insert Multiple Documents to MongoDB
Node.js URL
?
Node.js - Parse URL parameters
Node.js FS (File System)
?
Node FS
?
Node FS - Read a File
?
Node FS - Create a File
?
Node FS - Write to a File
?
Node FS - Append to a File
?
Node FS - Rename a File
?
Node FS - Delete a File
?
Node FS Extra - Copy a Folder
Node.js JSON
?
Node.js Parse JSON
?
Node.js Write JSON Object to File
Node.js Error Handling
?
Node.js Try Catch
Node.js Examples
?
Node.js Examples
?
Node.js - Handle Get Requests
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- php parse to string
- so you thought you were safe using angularjs think again owasp
- parse json tutorial kart
- convert json to buffer
- working with json in rpg scott klement
- url query string api documentation instantcard
- jeffrey richter guide to working with azure storage tables via c and
- on the privacy concerns of url query strings
- write json object to file example script
- mysql select from query examples tutorial kart