The JSON Data Type

MatrixOne supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column:

Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.

Automatically optimize storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document.

The space required to store a JSON document is roughly the same as for BLOB or TEXT.

JSON Types

JSON types contain JSON array and JSON object.

  • A JSON array contains a list of values separated by commas and enclosed within [ and ] characters:
  1. ["abc", 10, null, true, false]
  • A JSON object contains a set of key-value pairs separated by commas and enclosed within { and } characters:
  1. {"k1": "value", "k2": 10}

As the examples illustrate, JSON arrays and objects can contain scalar values that are strings or numbers, the JSON null literal, or the JSON boolean true or false literals. Keys in JSON objects must be strings. Temporal (date, datetime) scalar values are also permitted:

  1. ["12:18:29.000000", "2015-07-29", "2015-07-29 12:18:29.000000"]

Nesting is permitted within JSON array elements and JSON object key values:

  1. [99, {"id": "HK500", "cost": 75.99}, ["hot", "cold"]]
  2. {"k1": "value", "k2": [10, 20]}

Normalization of JSON Values

When a string is parsed and found to be a valid JSON document, it is also normalized. This means that members with keys that duplicate a key found later in the document, reading from left to right, are discarded.

Normalization is performed when values are inserted into JSON columns, as shown here:

  1. CREATE TABLE t1 (c1 JSON);
  2. INSERT INTO t1 VALUES
  3. ('{"x": 17, "x": "red"}'),
  4. ('{"x": 17, "x": "red", "x": [3, 5, 7]}');
  5. mysql> SELECT c1 FROM t1;
  6. +------------------+
  7. | c1 |
  8. +------------------+
  9. | {"x": "red"} |
  10. | {"x": [3, 5, 7]} |
  11. +------------------+