JSON Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(33 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://www.ietf.org/rfc/rfc4627.txt
=Internal=
=Internal=


* [[JSON#Subjects|JSON]]
* [[JSON#Subjects|JSON]]


=Top-Level Element=
=JSON Document=
The terms JSON document and JSON structure are used interchangeably. A JSON document consists in [[#Element|elements]] arranged in hierarchical structure, starting from the document root, or the [[#Top-Level_Element|top-level element]]. Each element is uniquely identified by a [[#Path|path]], starting from root.
 
==Element==
===Top-Level Element===
 
The top level element has always one of the [[#Data_Types|JSON data types]]. However, according to RFC4627, it seems that the a valid top level element can only be an [[#Object|Object]] <code>{...}</code> or an [[#Array|Array]] <code>[...]</code>, and noting else, so a string would not be a valid RFC4627 JSON content.


The top level element has always one of the following data types. Usually, it is an [[#Objects|Object]], so it starts with "{" or an [[#Arrays|Array]], so it starts with "[".
If it's a [[#Booleans|boolean]], [[#Numbers|number]] or [[#Strings|string]], the entire content consists of that sole element. For a complex, recursive content, the top element is either an [[#Objects|Object]], so it starts with "{" or an [[#Arrays|Array]], so it starts with "[".
==Path==
Each [[#Element|element]] of a JSON document is uniquely identified by a [[#Path|path]], starting from root. [[JSONPath#JSONPath_Expression|JSONPath expressions]] can be used to specify paths, or filters that select paths.


=Data Types=
=Data Types=
Line 38: Line 49:
</pre>
</pre>


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.
and they '''must''' be quoted. This constraint helps with semantics: if we see a number in a JSON context, we know it's a number, unless is quoted, in which case we know it's a string.
 
Always use double quotes ("), never use single quotes ('). Single quotes are only accepted if the parser was configured so, which is not the default. See "[[#Single_Quotes_vs._Double_Quotes|Single Quotes vs. Double Quotes]]" below.


==Numbers==
==Numbers==
Line 50: Line 63:
and they are not quoted.
and they are not quoted.


==Objects==
==<span id='Object'></span>Objects==


Objects are enclosed in curly brackets "{}" and contain [[#Field|fields]].
Objects are enclosed in curly brackets "{}" and contain [[#Field|fields]].
Line 67: Line 80:
</pre>
</pre>


==Fields==
===<span id='Field'><span>Fields===


A ''field'' is a key-value pair, where the key, or the ''field name'', is always a [[#Strings|string]].  
A ''field'' is a key-value pair, where the key, or the ''field name'', is always a [[#Strings|string]].  
Line 99: Line 112:
}  
}  
</pre>
</pre>


=Single Quotes vs. Double Quotes=
=Single Quotes vs. Double Quotes=
Line 106: Line 120:
=JSON Schema=
=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:
{{Internal|JSON Schema|JSON Schema}}
 
<pre>
{
  "description":"A customer",
  "type":"object",
  "properties":
  {
      "first": {"type": "string"},
      "last" : {"type" : "string"}
  }
}
</pre>
 
More http://json-schema.org.


=Collection+JSON=
=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/.
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/.
=Conversion between YAML and JSON=
{{Internal|Conversion between YAML and JSON|Conversion between YAML and JSON}}

Latest revision as of 00:24, 16 March 2021

External

Internal

JSON Document

The terms JSON document and JSON structure are used interchangeably. A JSON document consists in elements arranged in hierarchical structure, starting from the document root, or the top-level element. Each element is uniquely identified by a path, starting from root.

Element

Top-Level Element

The top level element has always one of the JSON data types. However, according to RFC4627, it seems that the a valid top level element can only be an Object {...} or an Array [...], and noting else, so a string would not be a valid RFC4627 JSON content.

If it's a boolean, number or string, the entire content consists of that sole element. For a complex, recursive content, the top element is either an Object, so it starts with "{" or an Array, so it starts with "[".

Path

Each element of a JSON document is uniquely identified by a path, starting from root. JSONPath expressions can be used to specify paths, or filters that select paths.

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. This constraint helps with semantics: if we see a number in a JSON context, we know it's a number, unless is quoted, in which case we know it's a string.

Always use double quotes ("), never use single quotes ('). Single quotes are only accepted if the parser was configured so, which is not the default. See "Single Quotes vs. Double Quotes" below.

Numbers

Numbers are represented as:

10

and they are not quoted.

Objects

Objects are enclosed in curly brackets "{}" and contain fields.

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

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

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

Fields

A field is a key-value pair, where the key, or the field name, is always a string.

The JSON RFC refers to the key as "names". Jackson refers to them as "field names".

The field name, is separated by the value by a semicolon (:). The value can have any of the JSON data types.

Fields can only exist inside of a JSON Object.

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 ("). Single quotes are only accepted if the parser was configured so, which is not the default.

JSON Schema

JSON Schema

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/.

Conversion between YAML and JSON

Conversion between YAML and JSON