JSON (
JavaScript Object Notation) is a text data format independent on platform. It is used for data transfer that can be organized into arrays or objects.
JSON represents any data structure (number, string, boolean, null, object or array composed of these) written in text form (text string). The hierachical complexity is unlimited in theory. The
JSON syntax is simultaneously a valid syntax of the
JavaScript language.
JSON data format is very simple, lightweight and easy to read. Compared to
XML the
JSON format is more efficient, readable and easier to process. Data structures written in
JSON are easy to use in
JavaScript itself, but also in other languages and environments including the PROMOTIC system.
JSON data types
-
String: Text string. The string must be entered into quotation marks (apostrophes are not allowed) and can contain all
Unicode characters. Quotation marks and backslash must be entered in the form
\" and
\\. Any character (the same way as in
JavaScript) can be entered also in the form
uXXXX, where XXXX represents the character code from
Unicode table written in hexadecimal.
Example:
"Temperature", "Name is \"PROMOTIC\""
- Number: Number, integer or real number (including the syntax with exponent). The decimal separator is always a period.
Example: 1316
, -1.23
, 0.12e-4
- Boolean: Logical value.
Example: true
, false
- Null: the null value (not set).
Example: null
- Array: Array (list of values). It is demarked by square brackets []. Array in JSON is a container with sequenced list of values. The values in array can be of any JSON data type including an object or an array.
Example:
[12, "Beethoven", 33.6, false]
[0.2, ["Mozart", "Wolfgang, "Amadeus", 1756], "Salzburg", 35.8, false]
-
Object: Object (name-value pairs). It is demarked by brackets
{}. Object in
JSON is not an object in the sense we know from
JavaScript. It is a container with data but no methods. Each data item (value) of the object has its key that is of the
String type. Other objects and arrays can be inserted into such object thus creating more complex tructures.
Example:
{"x": 100, "y": 100}
{"position": {"x": 100, "y": 100}, "size": {"dx": 200, "dy": 100}}
Values of other data types cannot be entered directly and must be transformed to some of the
JSON types. For example the date can be transformed into a text string or number in order to enter it to
JSON.
JSON itself is built on two universal structures:
- Collection of name-value pairs (
Object) - in the PROMOTIC system represented by the
PmMap object.
- Sorted list of values (
Array) - in the PROMOTIC system represented by the
PmArray object.
JSON support in the PROMOTIC system
The Pm.JsonParse method: Transforms
JSON format text into the object, array or elementary value
The Pm.JsonStringify method: From object, array or elementary value creates text in the
JSON format
Complex example:
JSON text syntax:
{"name":"BoilerRoom"; "count":2; "boilers": [{"temperature":36.3;"power":78}, {"temperature":12.6;"power":0}]}
is transformed by the
Pm.JsonParse method to a single variable (
PmMap object) containing properties:
name: String data type, value="BoilerRoom"
count: Double data type, Value=2
boilers: PmArray data type:
0th index: PmMap data type:
temperature: Double data type, Value=36.3
power: Double data type, Value=78
1st index: PmMap data type:
temperature: Double data type, Value=12.6
power: Double data type, Value=0