A basic utilization of JSON is to peruse information from a web server, and show the information in a web page.

For effortlessness, this can be shown by utilizing a string as contribution of a file).

JSON Example - Object From String

Create a JavaScript string containing JSON syntax:

var content = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

JSON sentence structure is a subset of JavaScript syntax.

The JavaScript work JSON.parse(text) can be utilized to change over a JSON content into a JavaScript object:

var obj = JSON.parse(text);

Use the new JavaScript object in your page:


<p id="demo"></p>

document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;

Using eval()

Older programs without the help for the JavaScript work JSON.parse() can utilize the eval() capacity to change over a JSON content into a JavaScript object:


var obj = eval ("(" + content + ")");

It is more secure to utilize a JSON parser to change over a JSON content to a JavaScript object.

A JSON parser will perceive just JSON message and won't incorporate contents.

In programs that give local JSON support, JSON parsers are likewise faster.

Native JSON support is incorporated into every single significant program and in the most recent ECMAScript (JavaScript) standard:

Web Browsers Support
  • Firefox 3.5
  • Internet Explorer 8
  • Chrome
  • Opera 10
  • Safari 4

For more seasoned programs, a JavaScript library is accessible at https://github.com/douglascrockford/JSON-js.

The JSON group was initially determined by ouglas Crockford