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
You escape a character by prepending a slash (\).
These characters can or must be escaped: "
. \
, /
, b
, f
, n
, r
, and t
And here are some common escape sequences in JSON:
\"
: Escaped double quote\\
: Escaped backslash\/
: Escaped forward slash (optional but often used)\b
: Backspace\f
: Form feed\n
: Newline\r
: Carriage return\t
: Horizontal tab\uXXXX
: Unicode escape sequence for special characters
Newlines in JSON
Just use the sequence: \\n
The escape sequence \n is used in JSON strings to represent newline characters, allowing text to span multiple lines.
\n
is really just two arbitrary characters right? An actual "newline" is just a single invisible character, but we represent it as \n
for convenience.
So in json you are simply putting in the magic character sequence: \n
in place of the real newline character.
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.
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
- ...
- MongoDB
- ...
- Virtuoso
- ...
Converting JSON to Other Formats
Ideally, you hold collections (MongoDB terminology) of JSON objects in JSON arrays.
I have tried to use jq
to convert to non-json formats. Extremely difficult. It is some good for object validations. But I didn't see I was making much headway towards my ultimate goal:
To convert JSON objects, or an array thereof, to C++/C initilization code.
I would like to turn:
[{"name": "Kate", "age": 22}, {"name": "Andrew", "age": 21}]into something like:
p.push_back("Kate", 22); p.push_back("Andrew", 21);Also, to initialize from objects themselves containing objects or arrays:
[{"name": "Kate", "age": 22, "children": ["Anne", "Tom"]}, {"name": "Andrew", "age": 21}]
After pondering the matter and searching the web, I have concluded that the most straightforward way is to use JavaScript. After all, JSON stands for JavaScript Object Notation!
Furthermore, you might test some JavaScript code embedded in a Web Page as a script, then move it to a node.js
programme.