JSON Objects
Object syntax
Example
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
null
}
A JSON object is written using curly braces {...}.
An object can contain multiple **key/value** pairs.
The key must be a string, and the value can be a valid JSON data type (string, number, object, array, boolean, or null).
A colon : is used to separate the key and the value.
Each key/value pair is separated by a comma.

Accessing object values
You can use a dot . to access the value of an object:
Example
var
myObj
,
x
;
myObj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
null
}
;
x
=
myObj
.
name
;
Try it »
You can also use square brackets ([]) to access the value of an object:
Example
var
myObj
,
x
;
myObj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
null
}
;
x
=
myObj
[
"
name
"
]
;
Try it »
Looping through an object
You can use for-in to loop through the properties of an object:
Example
var
myObj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
null
}
;
for
(
x
in
myObj
)
{
document
.
getElementById
(
"
demo
"
)
.
innerHTML
+=
x
"
"
;
}
Try it »
When looping through the properties of an object with for-in, use square brackets ([]) to access the property values:
Example
var
myObj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
null
}
;
for
(
x
in
myObj
)
{
document
.
getElementById
(
"
demo
"
)
.
innerHTML
+=
myObj
[
x
]
"
"
;
}
Try it »
Nested JSON objects
A JSON object can contain another JSON object:
Example
myObj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
sites
"
:
{
"
site1
"
:
"
www.runoob.com
"
,
"
site2
"
:
"
m.runoob.com
"
,
"
site3
"
:
"
c.runoob.com
"
}
}
You can use a dot . or square brackets [...] to access nested JSON objects.
Example
x
=
myObj
.
sites
.
site1
;
//
or
x
=
myObj
.
sites
[
"
site1
"
]
;
Try it »
Modifying values
You can use a dot . to modify the value of a JSON object:
Example
myObj
.
sites
.
site1
=
"
www.google.com
"
;
Try it »
You can use square brackets [...] to modify the value of a JSON object:
Example
myObj
.
sites
[
"
site1
"
]
=
"
www.google.com
"
;
Try it »
Deleting object properties
We can use the delete The keyword to delete a JSON object's property:
Example
delete
myObj
.
sites
.
site1
;
Try it »
You can use square brackets [...] to delete a JSON object's property:
Example
delete
myObj
.
sites
[
"
site1
"
]
;
Try it »