Parse JSON string in JavaScript using JSON.parse()

JSON or JavaScript Object Notation is a very lightweight data-interchange format. It’s also language independent and easy to understand. In this article, we will look into How to Parse JSON string in JavaScript using JSON.parse() method.

How to Parse JSON string in JavaScript using Json.parse()?


JSON.parse(text[, reviver])


text :-The string to parse as JSON. See the JSON object for a description of JSON syntax.

reviver :- Optional If a function, prescribes how the value originally produced by parsing is transformed, before being returned.

Return Value

Returns the object corresponding to the JSON text which is passed as a parameter

Examples of Parsing Simple JSON string in JavaScript and it’s output

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null

Complex JSON String Example:

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);

//output :-

// Object {employees: Array[3]}

Console Window Output of JSON.Parse()

Parse JSON string in JavaScript

Browser Support:

Most of the modern browsers support JSON.Parse() method which is defined in ECMA Script-262 5th Edition. According to Mozilla Developer Network below are the browsers that support JSON.Parse().


FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)3.5 (1.9.1)


FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support(Yes)(Yes)1.0 (1.0)(Yes)(Yes)(Yes)

Older Browser Workaround

There is a workaround for the older browser versions. You could implement the same functionality using  json2.js. The other option is to use eval() method to parse the JSON string into JavaScript object.

Example of eval() method to Parse JSON string

var strJSON = '{"result":true,"count":1}';
var objJSON = eval("(function(){return " + strJSON + ";})()");
console.log(objJSON.result);  //true
console.log(objJSON.count);   //1


Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…