JavaScript Object Notation (JSON) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript programming language, and it is a common data format used in REST APIs and for storing and exchanging data in web applications.
In this tutorial, you will learn how to work with JSON data in JavaScript. We will cover the following topics:
- Parsing JSON data
- Accessing JSON data
- Modifying JSON data
- Serializing JSON data
1. Parsing JSON data
To parse a JSON string in JavaScript, you can use the JSON.parse() method. This method parses a JSON string and returns a JavaScript object constructed from the JSON data.
For example, the following JSON string represents a user object with a name and an email address:
'{ "name": "John", "email": "[email protected]" }'
You can parse this JSON string in JavaScript like this:
let user = JSON.parse('{ "name": "John", "email": "[email protected]" }');
console.log(user);
The output of this code will be:
{ name: "John", email: "[email protected]" }
2. Accessing JSON data
Once you have parsed a JSON string and stored it in a JavaScript object, you can access the data stored in the object using dot notation or bracket notation.
For example, given the following JSON string:
'{ "name": "John", "email": "[email protected]", "age": 30 }'
You can access the individual values like this:
let user = JSON.parse('{ "name": "John", "email": "[email protected]", "age": 30 }');
console.log(user.name); // "John"
console.log(user.email); // "[email protected]"
console.log(user.age); // 30
You can also use bracket notation to access the values:
let user = JSON.parse('{ "name": "John", "email": "[email protected]", "age": 30 }');
console.log(user['name']); // "John"
console.log(user['email']); // "[email protected]"
console.log(user['age']); // 30
3. Modifying JSON data
To modify JSON data, you can simply change the values of the properties of the JavaScript object and then serialize the object back to a JSON string.
For example, given the following JSON string:
'{ "name": "John", "email": "[email protected]", "age": 30 }'
You can change the values of the properties like this:
let user = JSON.parse('{ "name": "John", "email": "[email protected]", "age": 30 }');
user.name = "Jane";
user.age = 35;
The user object will now have the following values:
{ name: "Jane", email: "[email protected]
4. Serializing JSON data
To convert a JavaScript object to a JSON string, you can use the JSON.stringify() method. This method takes a JavaScript object as input and returns a JSON string.
For example, given the following JavaScript object:
let user = {
name: "Jane",
email: "[email protected]",
age: 35
};
You can convert it to a JSON string like this:
let userJson = JSON.stringify(user);
console.log(userJson); // '{"name":"Jane","email":"[email protected]","age":35}'
You can also specify additional options as arguments to the JSON.stringify() method. For example, you can use the space option to add indentation to the JSON string:
let userJson = JSON.stringify(user, null, 2); console.log(userJson);
The output of this code will be:
{
"name": "Jane",
"email": "[email protected]",
"age": 35
}
In this tutorial, you learned how to work with JSON data in JavaScript. You learned how to parse JSON strings, access and modify JSON data, and serialize JavaScript objects to JSON strings. With these skills, you can effectively work with JSON data in your web applications.
