TypeScript JSON parse()
Syntax & Examples


JSON.parse() method

The parse() method of the JSON class in JavaScript 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.


Syntax of JSON.parse()

The syntax of JSON.parse() method is:

parse(text: string, reviver?: function): any

This parse() method of JSON converts a JavaScript Object Notation (JSON) string into an object.

Parameters

ParameterOptional/RequiredDescription
textrequiredThe string to parse as JSON. It must be a valid JSON string.
reviveroptionalA function that prescribes how the value originally produced by parsing is transformed, before being returned.

Return Type

JSON.parse() returns value of type any.



✐ Examples

1 Using parse() to convert a JSON string to an object

The parse() method can be used to convert a JSON string into a JavaScript object.

For example,

  1. Create a JSON string jsonString representing a person object.
  2. Use JSON.parse() to convert the JSON string into a JavaScript object.
  3. Store the resulting object in person.
  4. Log person to the console.

TypeScript Program

const jsonString = '{"name":"John", "age":30, "city":"New York"}';
const person = JSON.parse(jsonString);
console.log(person);

Output

{ name: 'John', age: 30, city: 'New York' }

2 Using parse() with a reviver function

The parse() method can be used with a reviver function to transform the parsed object before returning it.

For example,

  1. Create a JSON string jsonString representing a person object with a birthdate.
  2. Define a reviver function to convert the birthdate string into a Date object.
  3. Use JSON.parse() with the reviver function to convert the JSON string into a JavaScript object.
  4. Store the resulting object in person.
  5. Log person and the birthdate to the console.

TypeScript Program

const jsonString = '{"name":"John", "birthdate":"1990-01-01T00:00:00Z"}';
const person = JSON.parse(jsonString, (key, value) => {
  if (key === 'birthdate') {
    return new Date(value);
  }
  return value;
});
console.log(person);
console.log(person.birthdate);

Output

{ name: 'John', birthdate: 1990-01-01T00:00:00.000Z }
1990-01-01T00:00:00.000Z

3 Using parse() to handle nested JSON objects

The parse() method can be used to convert a JSON string representing nested objects into a JavaScript object.

For example,

  1. Create a JSON string jsonString representing a person object with an address.
  2. Use JSON.parse() to convert the JSON string into a JavaScript object.
  3. Store the resulting object in person.
  4. Log person and the address to the console.

TypeScript Program

const jsonString = '{"name":"John", "address":{"city":"New York", "zipcode":"10001"}}';
const person = JSON.parse(jsonString);
console.log(person);
console.log(person.address.city);

Output

{ name: 'John', address: { city: 'New York', zipcode: '10001' } }
New York

Summary

In this TypeScript tutorial, we learned about parse() method of JSON: the syntax and few working examples with output and detailed explanation for each example.