Javascript - Reference JSON Reference

JSON stands for JavaScript Object Notation)

JSON is a format for storing and transporting data.

JavaScript Objects can be converted into JSON, and JSON can be converted back into JavaScript Objects.

Demo

var myObj = { "name":"John", "age":31, "city":"New York" };
var myJSON = JSON.stringify(myObj);
console.log( myJSON );

Result

Valid Data Types

In JSON, values must be one of the following data types:

  • a string
  • a number
  • an object (JSON values. for nesting)
  • an array
  • a boolean
  • null

JSON values cannot be one of the following data types:

  • a function
  • a date
  • undefined

Demo

//Convert a string written in JSON format, into a JavaScript object.
var myJSON = '{ "name":"John", "age":31, "city":"New York" }';
var myObj = JSON.parse(myJSON);
console.log( myObj.name );// w ww .ja  v a2 s . c o  m

Result