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()?

Syntax 

Parameters

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

Complex JSON String Example:

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().

Desktop

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

Mobile

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

 

You may also like...

Leave a Reply

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