JSON/JSONB Types
JavaScript Object Notation (JSON) data can be a single scalar, an array, or a key-value pair object. The array and object can be called a container:
- Scalar: a number, Boolean, string, or null
- Array: defined in a pair of square brackets ([]), in which elements can be any type of JSON data, and are not necessarily of the same type.
- Object: defined in a pair of braces ({}), in which objects are stored in the format of key:value. Each key must be a string enclosed by a pair of double quotation marks (""), and its value can be any type of JSON data. In case of duplicate keys, the last key value will be used.
GaussDB offers two types for storing JSON data: JSON and JSONB. The JSON data type stores a complete copy of the input, retaining the entered spaces, duplicate keys, and sequence. The JSONB data type stores data in a decomposed binary form, removing semantic-irrelevant details and duplicate keys, and sorting key-values. Therefore, the JSONB data does not need to be parsed.
It can be found that both are of JSON type, and the same strings can be entered as input. The main difference between them is the efficiency. Because JSON data type stores an exact copy of the input text, the data must be parsed on every execution. In contrast, JSONB data is stored in a decomposed binary form and can be processed faster, though this makes it slightly slower to input due to the conversion mechanism. In addition, because the JSONB data type is normalized, it supports more operations, for example, comparing sizes according to a specific rule. JSONB also supports indexing, which is a significant advantage.
Input Format
An input must be a JSON-compliant string, which is enclosed in single quotation marks ('').
null (null-json): The value can only be null in lowercase.
gaussdb=# SELECT 'null'::json; -- suc json ------ null (1 row) gaussdb=# SELECT 'NULL'::jsonb; -- err ERROR: invalid input syntax for type json
Number (num-json): The value can be a positive or negative integer, decimal fraction, or 0. The scientific notation is supported.
gaussdb=# SELECT '1'::json; json ------ 1 (1 row) gaussdb=# SELECT '-1.5'::json; json ------ -1.5 (1 row) gaussdb=# SELECT '-1.5e-5'::jsonb, '-1.5e+2'::jsonb; jsonb | jsonb -----------+------- -0.000015 | -150 (1 row) gaussdb=# SELECT '001'::json, '+15'::json, 'NaN'::json; -- Redundant leading zeros, plus signs (+), NaN, and infinity are not supported. ERROR: invalid input syntax for type json
Boolean (bool-json): The value can only be true or false in lowercase.
gaussdb=# SELECT 'true'::json; json ------ true (1 row) gaussdb=# SELECT 'false'::jsonb; jsonb ------- false (1 row)
String (str-json): The value must be a string enclosed in double quotation marks ("").
gaussdb=# SELECT '"a"'::json; json ------ "a" (1 row) gaussdb=# SELECT '"abc"'::jsonb; jsonb ------- "abc" (1 row)
Array (array-json): Arrays are enclosed in square brackets ([]). Elements in the array can be any valid JSON data, and are unnecessarily of the same type.
gaussdb=# SELECT '[1, 2, "foo", null]'::json; json --------------------- [1, 2, "foo", null] (1 row) gaussdb=# SELECT '[]'::json; json ------ [] (1 row) gaussdb=# SELECT '[1, 2, "foo", null, [[]], {}]'::jsonb; jsonb ------------------------------- [1, 2, "foo", null, [[]], {}] (1 row)
Object (object-json): The value is enclosed in braces ({}). The key must be a JSON-compliant string, and the value can be any valid JSON string.
gaussdb=# SELECT '{}'::json; json ------ {} (1 row) gaussdb=# SELECT '{"a": 1, "b": {"a": 2, "b": null}}'::json; json ------------------------------------- {"a": 1, "b": {"a": 2, "b": null}} (1 row) gaussdb=# SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::jsonb; jsonb ----------------------------------------------------- {"foo": [true, "bar"], "tags": {"a": 1, "b": null}} (1 row)
- Note that 'null'::json and null::json are different, which are similar to the strings str="null" and str=null.
- For numbers, when scientific notation is used, JSONB expands them, while JSON stores an exact copy of the input text.
JSONB Advanced Features
- Precautions
- It cannot be used as a partition key.
- Foreign tables are not supported.
The main difference between JSON and JSONB lies in the storage mode. JSONB stores parsed binary data, which reflects the JSON hierarchy and facilitates direct access. Therefore, JSONB has many advanced features that JSON does not have.
- Format normalization
- After the input object-json string is parsed into JSONB binary, semantically irrelevant details are naturally discarded, for example, spaces:
gaussdb=# SELECT ' [1, " a ", {"a" :1 }] '::jsonb; jsonb ---------------------- [1, " a ", {"a": 1}] (1 row)
- For object-json, duplicate key-values are deleted and only the last key-value is retained. For example:
gaussdb=# SELECT '{"a" : 1, "a" : 2}'::jsonb; jsonb ---------- {"a": 2} (1 row)
- For object-json, key-values will be re-sorted. The sorting rule is as follows: 1. Longer key-values are sorted last. 2. If the key-values are of the same length, the key-values with a larger ASCII code are sorted after the key-values with a smaller ASCII code:
gaussdb=# SELECT '{"aa" : 1, "b" : 2, "a" : 3}'::jsonb; jsonb --------------------------- {"a": 3, "b": 2, "aa": 1} (1 row)
- After the input object-json string is parsed into JSONB binary, semantically irrelevant details are naturally discarded, for example, spaces:
- Size comparison
Format normalization ensures that only one form of JSONB data exists in the same semantics. Therefore, sizes may be compared according to a specific rule.
- First, type comparison: object-jsonb > array-jsonb > bool-jsonb > num-jsonb > str-jsonb > null-jsonb
- Content comparison if the data type is the same:
- str-jsonb: The default text sorting rule of the database is used for comparison. A positive value indicates greater than, a negative value indicates less than, and 0 indicates equal.
- num-jsonb: numeric comparison
- bool-jsonb: true > false
- array-jsonb: long elements > short elements. If the lengths are the same, compare each element in sequence.
- object-jsonb: If the length of a key-value pair is longer than that of a short key-value pair, the key is compared first, and then the value is compared.
For comparison within the object-jsonb type, the final result after format sorting is used for comparison. Therefore, the comparison result may not be intuitive compared with the direct input.
- Creating indexes, primary keys, and foreign keys
- Inclusion and existence
Querying whether a JSON contains some elements or whether some elements exist in a JSON is an important capability of JSONB.
-- Simple scalar/primitive values contain only the identical value. gaussdb=# SELECT '"foo"'::jsonb @> '"foo"'::jsonb; ?column? ---------- t (1 row) -- The array on the left contains the character string on the right. gaussdb=# SELECT '[1, "aa", 3]'::jsonb ? 'aa'; ?column? ---------- t (1 row) -- The array on the left contains all elements of the array on the right, regardless of the sequence and repetition. gaussdb=# SELECT '[1, 2, 3]'::jsonb @> '[1, 3, 1]'::jsonb; ?column? ---------- t (1 row) -- The object-json on the left contains all key-value pairs of object-json on the right. gaussdb=# SELECT '{"product": "PostgreSQL", "version": 9.4, "jsonb":true}'::jsonb @> '{"version":9.4}'::jsonb; ?column? ---------- t (1 row) -- The array on the left does not contain all elements of the array on the right. The three elements on the left are 1, 2, and [1,3], but the elements on the right are 1 and 3. gaussdb=# SELECT '[1, 2, [1, 3]]'::jsonb @> '[1, 3]'::jsonb; ?column? ---------- f (1 row) gaussdb=# SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"bar": "baz"}'::jsonb; ?column? ---------- f (1 row)
For details about the operators, see JSON/JSONB Functions and Operators.
- Functions and operators
For details about the functions and operators supported by the JSON/JSONB type, see JSON/JSONB Functions and Operators.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot