JSON Overview
JSON (JavaScript Object Notation) is a standard text-based data interchange format that enables applications to exchange data over a computer network. Programs written in Ruby, Java/EE, JavaScript, C#/.Net, PHP, etc. can easily consume and produce JSON data because it is independent of languages and computing platforms. The abundance of JSON-related APIs and tools make it easy to use JSON from your favorite programming language, IDE, and runtime environment. Additionlly, popular NoSQL databases such as MongoDB and CouchBase are based on JSON.
JSON was created by Douglas Crockford in 2001, and is specified in RFC 4627 with the IETF (Internet Engineering Task Force) standard; see http://tools.ietf.org/html/rfc4627. Per the specification, the JSON's IANA (Internet Assigned Numbers Authority) media type is application/json, and the file type is .json.
What is JSON?
JSON is a simple data format, and has 3 basic data structures:
- Name/Value (or Key/Value) Pair.
- Object.
- Arrays.
A valid JSON document is always surrounded with curly braces, like this:
{ JSON-Data }
Please note that some members of the JSON community use the term "string" rather than "document."
Why JSON?
JSON is gradually replacing XML as the preferred data exchange format on the internet because JSON is easy to read and its structures map to common programming concepts such as Objects and Arrays. JSON is more efficient (i.e., faster parsing and network transmission) than XML because JSON is more compact—there are no begin and end tags.
Name/Value Pair
A Name/Value pair looks like this:
{
"firstName": "John"
}
A property name (i.e., firstName) is a string that is surrounded by double quotes. A value can be a string (as in the above example), but this is just one of several valid data types. (Please see the Data Types section for further details.) Some well-known technologies claim that they use JSON data formats, but they don't surround their strings with quotes. However, this is not valid JSON; see the JSON Validation section.
Object
An Object is a collection of unordered Name/Value pairs. The following example shows an address object:
{
"address" : {
"line1" : "555 Main Street",
"city" : "Denver",
"stateOrProvince" : "CO",
"zipOrPostalCode" : "80202",
"country" : "USA"
}
}
An Object (in this case address) consists of comma-separated name/value pairs surrounded by curly braces.
Array
An Array is a collection of ordered values, and looks like this:
{
"people" : [
{ "firstName": "John", "lastName": "Smith", "age": 35 },
{ "firstName": "Jane", "lastName": "Smith", "age": 32 }
]
}
Value Types
A Value (i.e., the right-hand side of a Name/Value Pair) can be one of the following:
- Object
- Array
- String
- Number
- Boolean
- null
Number
A number can be an integer or double-precision float. Here are some examples:
"age": 29
"cost": 299.99
"temperature": -10.5
"speed_of_light": 1.23e11
"speed_of_light": 1.23e+11
"speed_of_light": 1.23E11
"speed_of_light": 1.23E+11
The property name (i.e., age, etc.) is a string surrounded by double quotes, but the value does not have quotes. A number can be prefixed by a minus sign. The exponent portion (denoted by e or E) comes after the number value, and can have an optional plus or minus sign. Neither leading zeroes, octal, nor hexadecimal values are allowed.
Boolean
A Boolean in JSON can either be true or false, as follows:
{
"emailValidated" : true
}
The property name (emailValid) is a string surrounded by double quotes, but the value (true) does not have quotes.
null
Although technically not a data type, null is a special value to indicate that a data element has no value. In the following example, the age field has no value (possibly because the user chose not to enter this information):
{
"age" : null
}
Comments
JSON does not allow comments. Comments were originally a part of JSON, but developers misused them by putting parsing directives in comments. When Douglas Crockford saw this practice, he removed comments from JSON to preserve interoperability between computing platforms.
Style
You've probably noticed that the property names (i.e., the name on the left-hand side of the colon) use camel case. This is not a rule or standard, but is a convention prescribed in Google's JSON Style Guide at: http://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml.
Official Syntax
Douglas Crockford's JSON site (http://www.json.org) provides a full description of JSON syntax.
Additionally, the JSON Pro Quick Guide (freely available in the iPhone App Store) provides examples and an overview of JSON syntax.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}