A JSON object is a set of unordered key-value pairs surrounded by curly braces {}. JSON objects are almost identical to JavaScript objects, which makes them incredibly natural to work with in web applications.
Click the access paths below to see how to retrieve data from a complex nested JSON object:
{
"user": "Alice",
"meta": {
"id": 505,
"role": "Moderator"
},
"site": "RedoHub"
}
JSON objects must be surrounded by curly braces. They contain key-value pairs separated by commas. Keys must be **double-quoted strings**, and values must be valid JSON data types.
{ "name":"John", "age":30, "city":"New York" }
You can access object values by using dot (.) notation or bracket ([]) notation.
let x = obj.name;
let x = obj["name"];
Objects can contain other objects as values. You can "chain" dot or bracket notation to reach deep into the hierarchy.
const myObj = {
"name":"John",
"cars": {
"car1":"Ford",
"car2":"BMW"
}
};
// Access nested property
console.log(myObj.cars.car2); // "BMW"
You can use either notation to modify any value in a JSON object after it has been parsed into a JavaScript object.
obj.name = "Jane";
obj["city"] = "London";
Use the delete keyword to remove a property from an object.
delete obj.city;
{}.