Parsing JSON in JavaScript

To parse JSON string in JavaScript, you can use the JSON.parse(string, function) method. The JSON.parse() method converts (or decodes) a string containing JSON data into a JavaScript object. The JSON.parse() method takes two parameters. The first parameter is the JSON string to parse, and the optional second parameter is the function used to transform the result (see the example below). If you pass an invalid JSON to the JSON.parse() method, you will get a SyntaxError exception. In this JSON Parse Example, we use the JSON.parse() method to convert a JSON into a JavaScript object. Click Execute to run the JavaScript JSON Parse Example online and see the result.
Parsing JSON in JavaScript Execute
let user = '{"name": "Jason", "age": 25, "friends": [0,1,2]}';

user = JSON.parse(user);

console.log(user.friends);
Updated: Viewed: 7260 times

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing structured data in a human-readable format. Compared to XML, JSON data takes up less space, is easier for humans to read, and for computers to process. JSON is frequently used to transfer data over the network in client/server and server/server communications. JSON itself has no methods and does not support comments; it's just a data storage format. JSON format came from JavaScript, but it is now actively used in almost all programming languages, including PHP, Python, Java, C++, C#, Go, and most of them, like Python, has built-in modules for working with JSON data.

How to decode JSON string in JavaScript?

The JSON.parse(string, reviver) method in JavaScript is intended for decoding (parsing) strings in JSON format. The JSON.parse() method returns an object, array, or elemental value resulting from parsing the passed JSON string. Note that the JSON object uses only double quotes (no single quotes or backticks). Also, JSON does not support comments, which makes it invalid. There is another JSON5 format that supports unquoted keys, comments, etc. But this is a standalone library, not a language specification. Plain JSON is strict because it makes it easy to implement robust and fast encoding and decoding algorithms.

JavaScript JSON.parse() Syntax
JSON.parse(string, reviver)

Where:
  • string: a string that contains data in JSON format
  • function (optional): a function that converts the parsed value before returning it from the method. The function will be called for each element while parsing the string
JavaScript Parse JSON String Example
let fruits = '["Banana", "Apple", "Orange", "Strawberry"]';

fruits = JSON.parse(fruits);

console.log(fruits[1]);

// output: Apple

How to convert values when parsing JSON?

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
let str = '{"name": "Jason", "age": 25, "city": "Tokyo"}';

let obj = JSON.parse(str, (key, value) => {
  if (typeof value === 'string') return value.toUpperCase();
  return value;
});

console.log(obj);

// output: {name: 'JASON', age: 25, city: 'TOKYO'}

How to convert a JavaScript object to a JSON string?

To convert JavaScript objects or arrays to a JSON string, you can use the JSON.stringify() method:

JavaScript JSON.stringify() Example
let user = {
  name: "Leo",
  level: 7
};

console.log(JSON.stringify(user))

// output: {"name":"Leo","level":7}

See also