JSON Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 38: Line 38:
</pre>
</pre>


and they ''must'' be quoted. Always use double quotes ("), never use single quotes (').
and they ''must'' be quoted. Always use double quotes ("), never use single quotes ('). Single quotes are only accepted if the parser was configured so, which is not the default.


==Numbers==
==Numbers==

Revision as of 18:53, 25 February 2017

Internal

Top-Level Element

The top level element has always one of the following data types. Usually, it is an Object, so it starts with "{" or an Array, so it starts with "[".

Data Types

JSON data types are: null, Booleans, Strings, Numbers, Objects (which can be thought of as Maps) and Arrays.

null

null is represented as a lowercase literal:

null

Booleans

Booleans are represented as lowercase literals:

true
false

and they are not quoted.

Strings

Strings are represented as:

"this is a string"

and they must be quoted. Always use double quotes ("), never use single quotes ('). Single quotes are only accepted if the parser was configured so, which is not the default.

Numbers

Numbers are represented as:

10

and they are not quoted.

Objects

Objects are enclosed in curly brackets "{}" and contain key (or "name")-value pairs. The JSON RFC refers to the key as "names".

The key and the value are separated by colon (":"). The pairs are delimited by commas (",").

The key is always a string, so it must be quoted.

{
  "id" : 42,
  "name" : "John Doe",
  "married" : true
  "kids" : [ "Alice", "Bob" ]
}

Arrays

Array elements are enclosed in brackets "[", and they are separated by commas:

[ "one element", "another element" ]

Comments

JSON does not support comments: https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaGSr

A comment can be simulated as follows:

{
   "comment":"this is a comment",

   ...

} 

Single Quotes vs. Double Quotes

Never use single quotes ('), always use double quotes (").

JSON Schema

JSON Schema is a vocabulary that allows to annotate and validate JSON documents. The schema describes the data format, and provides complete structural validation. Example:

{
  "description":"A customer",
  "type":"object",
  "properties":
   {
      "first": {"type": "string"},
      "last" : {"type" : "string"}
   }
}

More http://json-schema.org.

Collection+JSON

Collection+JSON is a JSON-based read/write hypermedia-type designed to support management and querying of simple collections. More details http://amundsen.com/media-types/collection/.