English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Online Tools
JSON Basics Tutorial
JSON objects can be created using JavaScript.A JSON object is enclosed in curly braces {}./Valuekey/written in the form of (key
keymust be a string, andvaluemust be a valid JSON data type.
keyThe colon (:) is used to separate 'value', and eachname/valueare separated by commas (,).
The following example shows how to create an object in JavaScript using JSON:
var myJSON = { "name":"Seagull", "age":32, "city":"New Delhi" };Test See‹/›
To access object values, we can use dot notation (.)
var myJSON = { "name":"Seagull", "age":22, "city":"New Delhi" }; myJSON.name; // returns "Seagull"Test See‹/›
We can also use bracket notation ([]) to access object values:
var myJSON = { "name":"Seagull", "age":22, "city":"New Delhi" }; myJSON["name"]; // returns "Seagull"Test See‹/›
We can use loops to iterate over object propertiesfor-in.
The following example retrieves each property of a JSON objectName:
var myJSON = { "name":"Seagull", "age":22, "city":"New Delhi" }; for(let x in myJSON) { document.getElementById("output").innerHTML += x; }Test See‹/›
The following example retrieves each property of a JSON objectValue:
var myJSON = { "name":"Seagull", "age":22, "city":"New Delhi" }; for(let x in myJSON) { document.getElementById("output").innerHTML += myJSON[x]; }Test See‹/›
Objects can be nested within other objects. Each nested object must have a unique access path.
var myJSON = { "name":"Seagull", "age":22, "pets": { "type":"dog", "name":"Oscar" } }
We can use dot notation (.) or bracket notation ([]) to access nested JSON objects:
myJSON.pets.name; /*** OR ***/ myJSON.pets["name"];Test See‹/›
We can use dot notation (.) to modify any value in a JSON object:
myJSON.age = 300; myJSON.pets.name = "Coco";Test See‹/›
We can also use bracket notation ([]) to modify object values:
myJSON["age"] = 300; myJSON.pets["name"] = "Coco";Test See‹/›
Use the delete keyword to remove properties from a JSON object.
delete myJSON.pets;Test See‹/›