JSON

From Omnia
Jump to navigation Jump to search

JSON

JSON - Wikipedia - http://en.wikipedia.org/wiki/Json

JSON (an acronym for JavaScript Object Notation pronounced /ˈdʒeɪsən/) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for most scripting languages.

The JSON format was originally specified by Douglas Crockford, and is described in RFC 4627. The official Internet media type for JSON is application/json. The JSON filename extension is .json.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is primarily used to transmit data between a server and web application, serving as an alternative to XML.

Data Types

JSON's basic types are:

  • Number (double precision floating-point format)
  • String (double-quoted Unicode with backslash escaping)
  • Boolean (true or false)
  • Array (an ordered sequence of values, comma-separated and enclosed in square brackets. The values don't need to have the same type.)
  • Object (an unordered collection of key:value pairs, comma-separated and enclosed in curly braces; the key must be a string)
  • null

Example

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, a number field for age, contains an object representing the person's address, and contains a list (an array) of phone number objects.

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
}

Format Pretty

echo '{"foo": "lorem", "bar": "ipsum"}' | jq
echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool

Source: linux - How to pretty-print JSON script? - Stack Overflow -http://stackoverflow.com/questions/352098/how-to-pretty-print-json-script

Other: Pretty print JSON from the command line « Ruslan’s Blog - http://ruslanspivak.com/2010/10/12/pretty-print-json-from-the-command-line/

Parsing

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

   A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
   An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

object.gif

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

array.gif

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

value.gif

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

string.gif

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

number.gif

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

Source: Introducing JSON - http://www.json.org/

Comments

There is no official "comment" syntax.

If you want, add something like a "_comment" field, that the processing application will ignore. Or dummy comment line:

{
  "############": "some comment",
  "data1": "value1"
...


Alternate hack:

"a little hack that allows you to place comments in a JSON file that will not affect the parsing, or alter the data being represented in any way. It appears that when declaring an object literal you can specify two values with the same key, and the last one takes precedence. Believe it or not, it turns out JSON parsers work the same way. So we can use this to create comments in the source JSON that will not be present in parsed object representation. Not all JSON parsers will understand this sort of JSON. Streaming parsers in particular may choke."
{
  "retry_interval" : "The interval in seconds between retrying failed API calls",
  "retry_interval" : 10,

  "auth_token" : "The authentication token. It is available in your developer dashboard under 'Settings'",
  "auth_token" : "5ad0eb93697215bc0d48a7b69aa6fb8b",
...

Some parsers:

"some parsers support c-style comments. "
// Configuration options
{
    /* Default encoding for text */
    "encoding" : "UTF-8",
...

References:

keywords