That is a Javascript object, specifically a object literal that is assigned to a variable.
The JSON form is the text that represent the object:
{
"countries": {
"country": [{
"cname": "Japan",
"capital": "Tokyo"
},
{
"cname": "India",
"capital": "Delhi"
}]
}
}
You can have the JSON as a string in the Javascript code, for example:
var json = '{"countries": {"country": [{"cname": "Japan","capital": "Tokyo"},{"cname": "India","capital": "Delhi"}]}}';
To turn a string containing JSON into a Javascript object, you would parse it. The JSON
object is available in recent browsers, where you can use the parse
method:
var j = JSON.parse(json);
To turn a Javascript object into JSON, you can use the stringify
method:
var json = JSON.stringify(j);
The JSON format was constructed as a subset of Javascript syntax so that you could easily parse it using the eval
method that was available in browsers at the time. As the eval
method will execute the string as code, that is a potential opening for cross scripting attacks, so you should use a method that parses the string instead of evaluating it when possible.