The standard way to parse JSON in JavaScript is JSON.parse()

The JSON API was introduced with ES5 (2011) and has since been implemented in >99% of browsers by market share, and Node.js. Its usage is simple:

const json = '{ "fruit": "pineapple", "fingers": 10 }';
const obj = JSON.parse(json);
console.log(obj.fruit, obj.fingers);


The only time you won't be able to use JSON.parse() is if you are programming for an ancient browser, such as IE 7 (2006), IE 6 (2001), Firefox 3 (2008), Safari 3.x (2009), etc. Alternatively, you may be in an esoteric JavaScript environment that doesn't include the standard APIs. In these cases, use json2.js, the reference implementation of JSON written by Douglas Crockford, the inventor of JSON. That library will provide an implementation of JSON.parse().

When processing extremely large JSON files, JSON.parse() may choke because of its synchronous nature and design. To resolve this, the JSON website recommends third-party libraries such as Oboe.js and clarinet, which provide streaming JSON parsing.

jQuery once had a $.parseJSON() function, but it was deprecated with jQuery 3.0. In any case, for a long time, it was nothing more than a wrapper around JSON.parse().

Answer from Andy E on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ parse
JSON.parse() - JavaScript | MDN
JSON.parse() parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression. The only instance where a piece of JSON text represents a different value from the same JavaScript expression is when dealing with the "__proto__" key โ€” see Object ...
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_parse.asp
JSON.parse()
Parse the data with JSON.parse(), and the data becomes a JavaScript object.
Discussions

Parse JSON in JavaScript? - Stack Overflow
The only time you won't be able to use JSON.parse() is if you are programming for an ancient browser, such as IE 7 (2006), IE 6 (2001), Firefox 3 (2008), Safari 3.x (2009), etc. Alternatively, you may be in an esoteric JavaScript environment that doesn't include the standard APIs. More on stackoverflow.com
๐ŸŒ stackoverflow.com
why is JSON.parse way slower than parsing a javascript object in the source itself?
These answers missed the most significant difference, run-time vs. compile time. There is a lot of overhead with parsing JSON since you are doing string parsing and constructing objects at runtime. The compiler cannot optimize this. Whereas, if you're loading a JS source file, the JIT compiler is able to kick in before the program is even executed. More on reddit.com
๐ŸŒ r/javascript
35
67
June 3, 2017
JSON response parsing in Javascript to get key/value pair - Stack Overflow
How can I get the name and value of each object in Javascript only? More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How to parse JSON using Node.js? - Stack Overflow
How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
ScrapingBee
scrapingbee.com โ€บ webscraping-questions โ€บ json โ€บ how-to-parse-a-json-file-in-javascript
How to parse a JSON file in JavaScript? | ScrapingBee
In JavaScript, you can parse a JSON string using the JSON.parse() method. A JSON file is essentially a text file containing a JSON string. Therefore, to read a JSON file, you first need to read the file as a string and then parse it into an ...
๐ŸŒ
jQuery
api.jquery.com โ€บ jQuery.parseJSON
jQuery.parseJSON() | jQuery API Documentation
Description: Takes a well-formed JSON string and returns the resulting JavaScript value. ... The JSON string to parse.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON
JSON - JavaScript | MDN
If the recipient of this message ... without producing a number value first (resulting in loss of precision) by using JSON.rawJSON() to precisely specify what the JSON source text should be....
Top answer
1 of 16
2044

The standard way to parse JSON in JavaScript is JSON.parse()

The JSON API was introduced with ES5 (2011) and has since been implemented in >99% of browsers by market share, and Node.js. Its usage is simple:

const json = '{ "fruit": "pineapple", "fingers": 10 }';
const obj = JSON.parse(json);
console.log(obj.fruit, obj.fingers);


The only time you won't be able to use JSON.parse() is if you are programming for an ancient browser, such as IE 7 (2006), IE 6 (2001), Firefox 3 (2008), Safari 3.x (2009), etc. Alternatively, you may be in an esoteric JavaScript environment that doesn't include the standard APIs. In these cases, use json2.js, the reference implementation of JSON written by Douglas Crockford, the inventor of JSON. That library will provide an implementation of JSON.parse().

When processing extremely large JSON files, JSON.parse() may choke because of its synchronous nature and design. To resolve this, the JSON website recommends third-party libraries such as Oboe.js and clarinet, which provide streaming JSON parsing.

jQuery once had a $.parseJSON() function, but it was deprecated with jQuery 3.0. In any case, for a long time, it was nothing more than a wrapper around JSON.parse().

2 of 16
107

WARNING!

This answer stems from an ancient era of JavaScript programming during which there was no builtin way to parse JSON. The advice given here is no longer applicable and probably dangerous. From a modern perspective, parsing JSON by involving jQuery or calling eval() is nonsense. Unless you need to support IE 7 or Firefox 3.0, the correct way to parse JSON is JSON.parse().

First of all, you have to make sure that the JSON code is valid.

After that, I would recommend using a JavaScript library such as jQuery or Prototype if you can because these things are handled well in those libraries.

On the other hand, if you don't want to use a library and you can vouch for the validity of the JSON object, I would simply wrap the string in an anonymous function and use the eval function.

This is not recommended if you are getting the JSON object from another source that isn't absolutely trusted because the eval function allows for renegade code if you will.

Here is an example of using the eval function:

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

If you control what browser is being used or you are not worried people with an older browser, you can always use the JSON.parse method.

This is really the ideal solution for the future.

Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ why is json.parse way slower than parsing a javascript object in the source itself?
r/javascript on Reddit: why is JSON.parse way slower than parsing a javascript object in the source itself?
June 3, 2017 -

I have 2MB of simple json (just an array of arrays) that I generate from a flask server and just dump into a javascript file to be executed by the browser. At first I did something like

var data = JSON.parse("{{json}}");

but then I realized I could just do

var data = {{json}};

for my simple data. I don't know if you can just dump json into javascript and get valid code, but I'm pretty sure that form my simple case it should work.

Here's my question: why does the first form take several seconds while the second is instantaneous (at least in chrome)? I would think that the parser for javascript would be more complex than the parser for JSON, and both would be implemented in native code, so where is this difference coming from?

๐ŸŒ
Apify
blog.apify.com โ€บ how-to-parse-json-in-javascript
How to parse JSON in JavaScript
March 6, 2024 - JavaScript objects are a fundamental part of the JavaScript language, while JSON is a separate and distinct data format. The JSON.parse() method is a built-in function that allows you to easily parse JSON data.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ vzx3pfwf โ€บ javascript-json-parse-example
How do I parse JSON in JavaScript?
JSON.parse() can take a function as its second parameter. A function can convert object values before they are returned. JavaScript Parse JSON String with function Example
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ what-is-difference-between-json-parse-and-json-stringify-methods-in-javascript
What is difference between JSON.parse() and ...
July 23, 2025 - JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages.
๐ŸŒ
Dustin John Pfister
dustinpfister.github.io โ€บ 2020 โ€บ 02 โ€บ 28 โ€บ js-json-parse
JSON parse method in javaScript with nodejs and client side examples | Dustin John Pfister at github pages
October 25, 2021 - The JSON parse method is a is then an inversion of the JSON stringify method is for turning a workable object into a JSON string. If you are new to javaScript a JSON string is a data interchange format that is often the default solution for such a thing in a javaScript environment.
๐ŸŒ
Json Parser Online
json.parser.online.fr
Json Parser Online
Analyze your JSON string as you type with an online Javascript parser, featuring tree view and syntax highlighting. Processing is done locally: no data send to server.
Top answer
1 of 4
154

There are two ways to access properties of objects:

var obj = {a: 'foo', b: 'bar'};

obj.a //foo
obj['b'] //bar

Or, if you need to dynamically do it:

var key = 'b';
obj[key] //bar

If you don't already have it as an object, you'll need to convert it.

For a more complex example, let's assume you have an array of objects that represent users:

var users = [{name: 'Corbin', age: 20, favoriteFoods: ['ice cream', 'pizza']},
             {name: 'John', age: 25, favoriteFoods: ['ice cream', 'skittle']}];

To access the age property of the second user, you would use users[1].age. To access the second "favoriteFood" of the first user, you'd use users[0].favoriteFoods[2].

Another example: obj[2].key[3]["some key"]

That would access the 3rd element of an array named 2. Then, it would access 'key' in that array, go to the third element of that, and then access the property name some key.


As Amadan noted, it might be worth also discussing how to loop over different structures.

To loop over an array, you can use a simple for loop:

var arr = ['a', 'b', 'c'],
    i;
for (i = 0; i < arr.length; ++i) {
    console.log(arr[i]);
}

To loop over an object is a bit more complicated. In the case that you're absolutely positive that the object is a plain object, you can use a plain for (x in obj) { } loop, but it's a lot safer to add in a hasOwnProperty check. This is necessary in situations where you cannot verify that the object does not have inherited properties. (It also future proofs the code a bit.)

var user = {name: 'Corbin', age: 20, location: 'USA'},
    key;

for (key in user) {
    if (user.hasOwnProperty(key)) {
        console.log(key + " = " + user[key]);
    }
}    

(Note that I've assumed whatever JS implementation you're using has console.log. If not, you could use alert or some kind of DOM manipulation instead.)

2 of 4
21

Try the JSON Parser by Douglas Crockford at github. You can then simply create a JSON object out of your String variable as shown below:

var JSONText = '{"c":{"a":[{"name":"cable - black","value":2},{"name":"case","value":2}]},"o":{"v":[{"name":"over the ear headphones - white/purple","value":1}]},"l":{"e":[{"name":"lens cleaner","value":1}]},"h":{"d":[{"name":"hdmi cable","value":1},{"name":"hdtv essentials (hdtv cable setup)","value":1},{"name":"hd dvd \u0026 blue-ray disc lens cleaner","value":1}]}'

var JSONObject = JSON.parse(JSONText);
var c = JSONObject["c"];
var o = JSONObject["o"];
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-parser
JSON Parser Online to parse JSON
JSON.Parse() is javascript method for parsing JSON which converts to JavaScript objects.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ javascript-tutorial โ€บ javascript-json-parsing.php
JavaScript JSON Parsing - Tutorial Republic
Tip: A data-interchange format is a text format which is used to interchange or exchange data between different platforms and operating systems. JSON is the most popular and lightweight data-interchange format for web applications. In JavaScript, you can easily parse JSON data received from the web server using the JSON.parse() method.
๐ŸŒ
Udacity
udacity.com โ€บ blog โ€บ 2021 โ€บ 02 โ€บ javascript-json-parse.html
JSON.parse() - Converting JSON into Javascript Objects | Udacity
September 27, 2022 - JSON parsing is the process of converting a JSON object to Javascript that can be used inside a program. Learn the standard method by using JSON.parse().
๐ŸŒ
JSON
json.org
JSON
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ what-is-the-difference-between-json-and-jsonparse
what is the difference between json() and json.parse()? (Example) | Treehouse Community
April 6, 2021 - json() is asynchronous and returns a Promise object that resolves to a JavaScript object. JSON. parse() is synchronous can parse a string to (a) JavaScript object(s).