Dirk Harriman Banner Image


Notes Javascript - JSON

Sub Menu Here



JSON - JavaScript Object Notation

The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON. It can't be called or constructed.


Unlike most global objects, JSON is not a constructor. You cannot use it with a new operator or invoke the JSON object as a function. All properties and methods of JSON are static (just like the Math object).

JavaScript & JSON Differences

JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax, but is distinct from JavaScript: most of JavaScript is not JSON. For example:

Any JSON text is a valid JavaScript expression, but only after the JSON superset revision. Before the revision, U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are allowed in string literals and property keys in JSON; but the same use in JavaScript string literals is a SyntaxError.

Other differences include allowing only double-quoted strings and no support for undefined or comments. For those who wish to use a more human-friendly configuration format based on JSON, there is JSON5, used by the Babel compiler, and the more commonly used YAML.

The same text may represent different values in JavaScript object literals vs. JSON as well.

Sample JSON

{ "browsers": { "firefox": { "name": "Firefox", "pref_url": "about:config", "releases": { "1": { "release_date": "2004-11-09", "status": "retired", "engine": "Gecko", "engine_version": "1.7" } } } } }


The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

You can use the JSON.parse() method to convert the above JSON string into a JavaScript object:

const jsonText = `{ "browsers": { "firefox": { "name": "Firefox", "pref_url": "about:config", "releases": { "1": { "release_date": "2004-11-09", "status": "retired", "engine": "Gecko", "engine_version": "1.7" } } } } }`; var jsonObj = JSON.parse(jsonText);

Sample JSON & Parse

const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); console.log(obj.count); // EXPECTED OUTPUT: 42 console.log(obj.result); // EXPECTED OUTPUT: true