TypeScript JSON parse()

In this chapter, we will explore the JSON.parse() method in TypeScript. This method parses a JSON string and constructs the JavaScript value or object described by the string. Understanding how to use JSON.parse() is essential for working with JSON data in TypeScript.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The JSON.parse() method parses a JSON string and constructs the JavaScript value or object described by the string. It can also optionally transform the result with a function.

2. Syntax

JSON.parse(text, reviver?);

Parameters

  • text: A valid JSON string.
  • reviver (optional): A function that can transform the result. This function is called for each member of the object.

Return Value

The method returns the JavaScript value or object corresponding to the provided JSON string.

3. Examples

Let's look at some examples to understand how JSON.parse() works in TypeScript.

Example 1: Basic Usage

In this example, we use JSON.parse() to parse a JSON string into a JavaScript object.

let jsonString = '{"name": "Ravi", "age": 25}';
let parsedObject = JSON.parse(jsonString);
console.log(parsedObject); // Output: { name: 'Ravi', age: 25 }

Example 2: Parsing Nested JSON

In this example, we use JSON.parse() to parse a nested JSON string into a JavaScript object.

let jsonString = '{"name": "Ravi", "address": {"city": "Mumbai", "zip": "400001"}}';
let parsedObject = JSON.parse(jsonString);
console.log(parsedObject); // Output: { name: 'Ravi', address: { city: 'Mumbai', zip: '400001' } }

Example 3: Using the Reviver Function

In this example, we use JSON.parse() with a reviver function to transform the parsed result.

let jsonString = '{"name": "Ravi", "age": "25"}';
let parsedObject = JSON.parse(jsonString, (key, value) => {
  if (key === "age") {
    return parseInt(value);
  }
  return value;
});
console.log(parsedObject); // Output: { name: 'Ravi', age: 25 }

Example 4: Handling Invalid JSON

In this example, we demonstrate how JSON.parse() throws an error for invalid JSON strings.

let jsonString = '{"name": "Ravi", "age": 25,}'; // Invalid JSON due to trailing comma
try {
  let parsedObject = JSON.parse(jsonString);
  console.log(parsedObject);
} catch (error) {
  console.log("Error parsing JSON:", error); // Output: Error parsing JSON: SyntaxError: Unexpected token } in JSON at position 25
}

Example 5: Parsing JSON Array

In this example, we use JSON.parse() to parse a JSON string representing an array.

let jsonString = '["apple", "banana", "mango"]';
let parsedArray = JSON.parse(jsonString);
console.log(parsedArray); // Output: [ 'apple', 'banana', 'mango' ]

Example 6: Parsing Complex JSON

In this example, we use JSON.parse() to parse a complex JSON string.

let jsonString = '{"students": [{"name": "Ravi", "age": 25}, {"name": "Ankit", "age": 30}]}';
let parsedObject = JSON.parse(jsonString);
console.log(parsedObject);
// Output:
// { students:
//   [ { name: 'Ravi', age: 25 },
//     { name: 'Ankit', age: 30 } ] }

4. Conclusion

In this chapter, we explored the JSON.parse() method in TypeScript, which is used to parse a JSON string and construct the JavaScript value or object described by the string. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use JSON.parse() effectively is essential for working with JSON data in TypeScript applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare