Code | Description | Example |
---|---|---|
parse |
JSON parser | let json = JSON.parse(<json_string>); |
stringify |
JSON stringifier | let json = JSON.stringify(<json_object>); |
obj[] = |
Add value | <object>[<new_key>] = <value>; |
obj[] |
Get value | let value = <object>[<key>]; |
obj[] |
Update value | <object>[<key>] = <value>; |
delete |
Delete value | delete <object>[<key>]; |
-
JSON (JavaScript Object Notation) is:
- a text file with the
.json
extension - a lightweight format for storing and transporting data.
- a string format that is easy to parse and manipulate.
- mainly used for sending and receiving data between applications annd servers.
- a text file with the
-
JS objects are:
- created with curly brackets (
{}
) inside a JS file - a data structure that is used to store key-value pairs.
- mainly used for storing data in JavaScript applications.
- created with curly brackets (
Example:
// JSON (string format)
/* example.json */
{
"name": "John",
"age": 30
"pets": [
"cat",
"dog"
]
}
// JS object (key-value pairs)
let jsObject = { name: "John", age: 30, pets: ["cat", "dog"] };
Use JSON.parse
to parse JSON.
let obj = JSON.parse(<json_string>);
<json_string>
is the string you want to parse.
Example:
let obj = JSON.parse('{"name": "John", "age": 30}');
console.log(obj); // Output: { name: "John", age: 30 }
Use JSON.stringify
to stringify JSON.
let json = JSON.stringify(<json_object>);
<json_object>
is the object you want to stringify.
Example:
let json = JSON.stringify({ name: "John", age: 30 });
console.log(json); // Output: '{"name":"John","age":30}'
Use <object>[<new_key>] = <value>
to add value to JSON.
<object>[<new_key>] = <value>
<object>
is the object you want to add value to.<new_key>
is the new key you want to add value to.<value>
is the value you want to add.
Example:
let obj = {
name: "John",
age: 30,
};
obj["city"] = "New York";
console.log(obj); // Output: { name: "John", age: 30, city: "New York" }
Use <object>[<key>]
to get value from JSON.
<object>[<key>]
<object>
is the object you want to get value from.<key>
is the key you want to get value from.
Example:
let obj = {
name: "John",
age: 30,
};
console.log(obj.name); // Output: "John"
Use <object>[<key>] = <value>
to update value in JSON.
<object>[<key>] = <value>
<object>
is the object you want to update value in.<key>
is the key you want to update value in.
Example:
let obj = {
name: "John",
age: 30,
};
obj.age = 31;
console.log(obj); // Output: { name: "John", age: 31 }
Use delete <object>[<key>]
to delete value from JSON.
delete <object>[<key>]
<object>
is the object you want to delete value from.<key>
is the key you want to delete value from.
Example:
let obj = {
name: "John",
age: 30,
};
delete obj.age;
console.log(obj); // Output: { name: "John" }