JavaScript JSON.parse()
The JSON.parse() method is used to convert a JSON string into an object.
Syntax
JSON.parse(text[, reviver])
Parameter Description:
- **text:** Required, a valid JSON string.
- reviver: Optional, a function that transforms the result; this function is called for each member of the object.
Return Value:
Returns the object converted from the given JSON string.
Example
<
p
id=
"demo"
<
/p
<
scriptdocument.getElementById("demo").innerHTML =
obj.employees[1].name + " " + obj.employees[1].site;
<
/script
Try it »
Using the optional parameter:
Example
JSON
.
parse
(
'
{"p": 5}
'
,
function
(
k
,
v
)
{
if
(
k
===
'
'
)
{
return
v
;
}
return
v
2
;
}
)
;
JSON
.
parse
(
'
{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}
'
,
function
(
k
,
v
)
{
console
.
log
(
k
)
;
//
Outputs the current property, the last one is ""
return
v
;
//
Returns the modified value
}
)
;
Try it »
JavaScript JSON