JSON - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSON Node js

JSON Node js

shape Introduction

Befor going to work with JSON parsing with node.js first user need to node about node.js. Node.js is a framework to develop highly-scalable applications which can deal with a huge number of simultaneous client connections proficiently. Node.js is based on the V8 JavaScript Engine. The scalable network programs can be built easily using Node.js. So, Node.js is used for developing high traffic web applications. Following are some of the types of applications which can be developed using Node.js.

shape Conceptual figure

The figure below demonstrates the Architecture of Node.js which uses "Single Threaded Event Loop Model". Following are some of the advantages of Node.js. Following are some of the Disadvantages of Node.js.

shape Example

The example demonstrates a simple node server that outputs a message or greeting using a text editor. [c] var http = require('http'); var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("SPLessons stands for simple programing languages \n"); }); // IP defaults to 127.0.0.1, listen on port 8080 server.listen(8080); //give a message on terminal console.log("Server running at http://127.0.0.1:8080/"); [/c] Node.js gives a module framework with a huge developer group. Node.js projects can stack singular modules utilizing the require() strategy. While numerous modules needs to be downloaded and some modules such as http are incorporated with Node.js establishments. The HTTP server is made utilizing the http module's createServer() strategy. The createServer() takes a callback work as a parameter. This callback capacity is executed every time the server gets another request. The function callback will take two contentions, request and response. a request contains data with respect to the browsers such as URL, HTTP headers. Thus, the response is utilized to return information back to the browser. The function callback starts by calling the response.writeHead() strategy. This technique sends a HTTP status code and an accumulation of response headers back to the browser. The status code is utilized to show the result of the client request. Output: Now compile the code result will be as follows.

Parsing JSON In Node.js

shape Description

By using require() function the developer can load JSON file into the Node.js . Now lets take two files such as config.js and config.json. config.js [html] module.exports = { server: "localhost", port: 1234, timeout: 10 }; [/html] config.json [html] { "server": "localhost", "port": 1234, "timeout": 10 } [/html] Now above two will be read out by using require() as follows. [html] // When loading a configuration file, we have the choice to load either .js file, // which will be interpreted as a JavaScript module file, with the "exports" being // returned from the require; or, we can load a .json file, which will be parsed as // JavaScript Object Notation (JSON) with the result being returned from the require. // Load configuration as JSON. console.log( "JSON File:" ); console.log( require( "./config.json" ) ); // Load configuration as module. console.log( "MODULE File:" ); console.log( require( "./config.js" ) ); [/html] Output: Now compile the code result will be as follows.
ben$ node explicit-ext.js JSON File: { server: 'localhost', port: 1234, timeout: 10 } MODULE File: { server: 'localhost', port: 1234, timeout: 10 }

JSON Object To String

By using the built-in global a string will be converted into JSON Object as follows. [html] var jsonString = "{\"key\":\"value\"}"; var jsonObj = JSON.parse(jsonString); console.log(jsonObj.key); [/html]

String To JSON Object

By using the stringify method a JSON Object will be converted into string as follows. [html] var jsonObj = {'key':'value'}; console.log(JSON.stringify(jsonObj)); [/html]

Read JSON From File System In NodeJS

The following is the code. [html]var jsonObj = require("./path/to/myjsonfile.json");[/html] Here, NodeJS consequently read the document, parse the substance to a JSON protest and gives that to left side of the variable.

Add New Element To JSON Object

The following is the code to add the new element to the existing JSON object. [html] var myJson = {'key':'value'}; //new element myJson.key2 = 'value2'; //or myJson[key3] = 'value3'; [/html]

Delete An Element From A JSON Object

The following is the code to delete the element from the JSON object. [html] var myJson = {'key':'value'}; delete myJson['key']; [/html]

Summary

shape Key Points

  • The node.js interpreter interprets and executes the javaScript code Written in source file.
  • Node.js is based on V8 JavaScript engine.
  • Node.js uses “Single Threaded Event Loop Model”
  • Node.js does not use Event Loop as a library.
  • Node.js is a framework used to develop a highly-scalable applications.