JSON Data

www.altova.com Expand/Collapse All Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > JSON, JSON Schema >

JSON Data

This section contains a brief description of how JSON data is structured. JSON data is typically stored in a JSON (instance) document but can also be stored as a JSON data fragment in a document of another type. A JSON data fragment or document is a JSON data structure, which is broadly defined as set out below.

 

XMLSpy additionally supports JSON5, which is an extension of JSON that adds some minimal ECMScript 5 extensions. See json5.org for more information.

 

JSON objects and arrays

A JSON document (saved typically with the file extension .json) is built on the following core data structures:

 

 

Object

An object is delimited by curly braces, and is an unordered collection of zero or more key:value pairs. These key:value pairs are the properties of the object. The key must always be a string and must therefore always be enclosed in quotes. The key (also called the name of the property) is separated from its value by a colon. A property value can be of any JSON datatype (see list below). A property is separated from the next by a comma. The listing below is an example of an object with three properties (all of which have atomic-type values):

 

    {

      "emailtype": "home",

      "emailaddress": "contact01.home@altova.com",

      "citycode": 22

    }

 

 

 

Array

An array is delimited by square brackets, and is a comma-separated ordered list of zero or more items. These items can be of any JSON datatype (see list below).

 

Click to expand/collapseAn array containing two objects

The array below consists of two objects (each enclosed in curly braces). The array itself is indicated with square brackets.

 

[

      {

      "emailtype": "home",

      "emailaddress": "contact01.office@altova.com",

      "citycode": 22

      },
      {

      "emailtype": "office",

      "emailaddress": "contact01.office@altova.com",

      "citycode": 22

      }

    ]

 

 

Click to expand/collapseArrays that are the values of an object's properties

The listing below is of an object with three key:value pairs. Each value is an array that contains a tuple (sequence). (A tuple can be considered to be a one-dimensional array.)  The three items in each tuple are atomic types.

 

{

      "x": [ 1, 2, "abc" ],

      "y": [ 3, 4, "def" ],

      "z": [ 5, 6, "ghi" ]

}

 

 

JSON data types

Object property values and array items can be of the following types:

 

string (must be enclosed in quotes). A string can additionally be specified to have a format, such as a date-time or email format
number: A number with a fractional part; it includes integers
integer: A number with no fractional part; a subset of the number type
boolean (true/false, not enclosed in quotes)
object: When used within another object, allows data to be nested
array: Provides the ability to build more complex structures than allowed by objects
null (null, not enclosed in quotes)

 

Example of JSON data

Here is an example of a JSON data fragment. Note how the document is structured into objects and arrays. Also note the data type of key values; string values are in quotes, other types are colored green.

 

{

  "first": "Jason",

  "last": "Jones"

  "isManager": true,

  "age": 35,

  "address": {

    "street": "Jason Avenue",

    "city": "Jasonville",

    "state": "JS",

    "postcode": "JS12 ON34"

  },

  "phone": [

    {

      "type": "home",

      "number": "12 3456-7890"

    },

    {

      "type": "office",

      "number": "789 012-34567"

    }

  ],

  "children": [],

  "partner": null

}

 

Some differences between JSON5 and JSON

JSON5 is a strict subset of JavaScript, adds no new JSON data types, and works with all existing JSON content. Some notable differences are listed below:

 

JSON5 supports comments. Comments are delimited like this: // comment // or /* comment */.
In JSON5, the keys of key:value pairs do not need to be enclosed in quotes.
In JSON5, strings can be written across multiple lines.
JSON5 documents can be validated against JSON schemas.

 


© 2019 Altova GmbH