JSON stands for JavaScript Object Notation. JSON is a human-readable data format commonly used to exchange data between web browser, clients and server. Most of the modern APIs using JSON formats as output. That’s why the JSON is becoming popular data format for the API’s output.

JavaScript provides two methods to work with JSON content. parse and stringify. The methods are JSON.parse() and JSON.stringify().

  • JSON.parse() method takes JSON string and transforms it into a JavaScript object.
  • JSON.stringify() method takes a JavaScript object and transforms it into a JSON string.

1. Using JSON.parse()

The JSON.parse() function takes input a JSON data and transforms it into a JavaScript object. Here is a simple example of converting a JSON string into a JS object.


_// Store JSON data in a JavaScript variable_

**var**    json   =    '{"id": 1, "name": "Dan Radak", "country": "United States"}' ;

_// Convert JSON string to JavaScript object_

**var**   obj   =   JSON . parse (json );

_// Access individual values from the JavaScript object_

console . log (obj .id );  _// Outputs: 1_

console . log (obj .name );  _// Outputs: Dan Radak_

console . log (obj .country );  _// Outputs: United States_

#general articles #javascript #js #json

How to Use JSON.parse() and JSON.stringify()
4.15 GEEK