🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Definition
- Syntax
- Examples
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment