JSON Object


Object Syntax

Example

{ "name":"example", "alexa":10000, "site":null }

JSON objects are written in{...}curly braces.

Objects can contain multiplekey/valuepairs.

Keys must be strings, and values can be valid JSON data types (string, number, object, array, boolean, or null).

Use a colon between the key and value:to separate them.

Each key/value pair uses a comma,to separate.


Access Object Values

You can use a dot.to access the object's values:

Example

var myObj, x; myObj = { "name":"example", "alexa":10000, "site":null }; x = myObj.name;

Try it Yourself »

You can also use square brackets ([]) to access the object's values:

Example

var myObj, x; myObj = { "name":"example", "alexa":10000, "site":null }; x = myObj["name"];

Try it Yourself »

Loop Object

You can use for-in to loop through the properties of an object:

Example

var myObj = { "name":"example", "alexa":10000, "site":null }; for (x in myObj) { document.getElementById("demo").innerHTML += x + "<br>"; }

Try it Yourself »

When looping through object properties with for-in, use square brackets ([]) to access the property values:

Example

var myObj = { "name":"example", "alexa":10000, "site":null }; for (x in myObj) { document.getElementById("demo").innerHTML += myObj[x] + "<br>"; }

Try it Yourself »

Nested JSON Object

JSON objects can contain another JSON object:

Example

myObj = { "name":"example", "alexa":10000, "sites": { "site1":"www.example.com", "site2":"m.example.com", "site3":"c.example.com" } }

You can use dot notation.or square brackets[...]to access nested JSON objects.

Example

x = myObj.sites.site1; //or x = myObj.sites["site1"];

Try it Yourself »

Modify Values

You can use a dot.to modify the value of a JSON object:

Example

myObj.sites.site1 = "www.google.com";

Try it Yourself »

You can use square brackets[...]to modify the value of a JSON object:

Example

myObj.sites["site1"] = "www.google.com";

Try it Yourself »

Delete Object Properties

We can use thedeletekeyword to delete JSON object properties:

Example

delete myObj.sites.site1;

Try it Yourself »

You can use square brackets[...]to delete JSON object properties:

Example

delete myObj.sites["site1"]

Try it Yourself »
Other Extensions