JavaScript Object Notation (JSON)
JSON (JavaScript Object Notation) is a text-based, human-readable data interchange format used to exchange data between web clients and web servers. The format defines a set of structuring rules for the representation of structured data. JSON is used as an alternative to Extensible Markup Language (XML), but unlike XML, JSON is simply a way to represent data structures, as opposed to a full markup language.
The MIME type for JSON text is application/json
JSON Syntax
JSON consists of atomic types (strings, numbers--either integers or decimal--, Booleans, and null
), as well as arrays and objects.
A JSON object consists of name and value pairs, where the name is a string and the value is either atomic or composite (array or object) type, comma separated and enclosed in curly brackets.
JSON is written in name and value pairs very much like JavaScript object properties.
A name and value pair is constructed using a name that is placed in double quotes, followed by a colon and a given value.
For example:
{ name: "John", born: 1966, married: false, children: ["Monica", "Chris"], wife: null}
Punctuation used in the JSON format includes quotations, curly and square brackets, parentheses, commas and colons.
JSON objects
JSON objects are unordered sets of name and value pairs. Objects are written inside of curly braces, like these { }. Everything inside the curly braces is part of the object. Objects can contain multiple name and value pairs. Each name is followed by a colon, and name value pairs are separated by a comma.
JavaScript objects can be accessed when needed and modified, deleted or looped.
JSON arrays
JSON arrays are an ordered list of values. Arrays are used to store objects, strings, number notation and Boolean notation. An array can be made up of multiple data types.
Arrays in JSON are surrounded by square brackets, like these [ ]. Each value in the array is separated by a comma. In JavaScript, programmers can access array values and update, delete or loop them. An array can be stored inside another JSON array; that's called a multidimensional array.
A JSON object may contain another object, or even an array of objects (see next).
For example, an array of employee names may look like this:
"animals":[ {"kind":"horse", "legs": 4}, {"kind":"spider", "legs": 8} ]
Each line is an object, and both lines together would be part of an array. Names in the name and value pairs include kind and legs, while the value pairs would be the actual appearing names, like horse and spider.
Escaping Characters in JSON*
JSON vs. JavaScript
In JavaScript, an object may also map to:
- a function
- a date
undefined
Also, in JSON, string values must be written between double quotes:
{"name":"John"}
In JavaScript, you can write string values with double or single quotes, as in {name:'John'}
.
JSON vs. XML
JSON is faster than XML because it is designed specifically for data interchange. JSON encoding is terse, which requires fewer bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.
JSON is a more compact format than XML, meaning it weighs far less on the wire than the more verbose XML. JSON is easier to work with in some languages (such as JavaScript, python, and php). Formatted JSON is generally easier to read than formatted XML.
Also, the JSON format is syntactically identical to the code for creating JavaScript objects. Because of this similarity, a JavaScript program can easily convert JSON data into native JavaScript objects.
Characters to be Escaped
You escape a character by prepending a slash (\).
These characters can or must be escaped:
- "
- \
- /
- b
- f
- n
- r
- t
- u hex hex hex hex
JSON Schema Validation*
JSON Query tools available in linux
From simplest to most complex and complete
- jq
- a command-line JSON processor
- jmespath
- whose jp command is a command-line interface to JMESPath, an expression language for manipulating JSON (as XPath is to XML).
- taoJSON
-
A zero-dependency C++ header-only JSON library that provides a generic Value Class, uses Type Traits to interoperate with C++ types, uses an Events Interface to convert to and from JSON, JAXN, CBOR, MsgPack and UBJSON, and much, much more.
Files are located in the following directories:
- Headers (*.hpp) in directory: /usr/include/tao/json/ and subdirectories
- Examples (*.cpp) in directory: /usr/share/doc/tao-json-examples
- CouchDB
- ...
- Virtuoso
- ...