A JSON Object is a textual representation of an object in JSON format as defined by RFC 7159 section 4; it is an unordered collection of name/value pairs (called "object members"), where names are JSON Strings and values can be any JSON value.
A JSON Object is a textual representation of an object in JSON format as defined by RFC 7159 section 4; it is an unordered collection of name/value pairs (called "object members"), where names are JSON Strings and values can be any JSON value.
Example (more below): {"answer":42}
Once the JSON text is parsed, the type of the resulting structure will vary widely depending on the environment being used. Some languages such as JavaScript, Python, and Ruby have builtin support for (nested) "dictionaries" and so the result will be a native structure. Other languages such as C, C++ and Java do not have such builtin types and represent the parsed result using dedicated classes.
Notes:
- A member name can be an empty string; this is valid JSON:
{ "": null }
. - Duplicate member names are allowed but the behaviour of a JSON parser in this case is undefined; for instance, in
{ "key": "v1", "key": "v2" }
, there is no guarantee as to what the JSON parser will produce as a value for"key"
. - JSON is a textual notation for data exchange. If you're writing source code and not dealing with a string, you're not dealing with JSON anymore, you're dealing with the result of parsing JSON. This can be confusing in languages that can't map a JSON Object to a built-in structure, like Java, as the names of the classes involved can be confusing (
JSONObject
in the case of many JSON libraries for Java). Despite the name, it's no longer JSON; it's just the result of parsing JSON. - JavaScript object initializers are not the same as JSON Objects, e.g., this is not JSON:
var obj = {"foo":"bar"};
JavaScript object initializers have slightly different semantics than JSON (for instance, member names need not be in quotes, values that do not exist in JSON such asundefined
can be included, duplicate member names have a defined result both in loose and strict mode, member names can be the result of a calculation [in ES2015 and later], etc.).
Examples:
An object with one member, called answer
, whose value is 42:
{"answer":42}
An identical object with whitespace that's ignored in JSON:
{
"answer" : 42
}
An object with two members:
{"answer":42,"question":"Life, the Universe, and Everything"}
An object with another object as the value of one of its members:
{
"title": "The Hitchhiker's Guide to the Galaxy",
"author": {
"name": "Douglas Adams",
"website": "http://douglasadams.com/",
"wikipedia": "https://en.wikipedia.org/wiki/Douglas_Adams"
}
}