JSONB - Postgresql
JSONB - Postgresql
1. Why
2. Operators for handling Jsonbs
3. Migration example
JSON syntax
JSON vs JSONB
JSON data types are for storing JSON (JavaScript Object Notation) data,
PostgreSQL offers two types for storing JSON data: json and jsonb
While jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly
faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage.
RULES:
1. The contained object must match the containing object as to structure and data contents
2. The order of array elements is not significant when doing a containment match
3. Duplicate array elements are effectively considered only once
? operator:
It tests whether a string (given as a text value) appears as an object key or array element at the top level of the jsonb value
@> operator
Extracts n'th element of JSON array (array elements are indexed from zero, but negative integers count from the end
-> operator
json -> text → json
jsonb -> text → jsonb
Extracts JSON object field with the given key.
->> operator
Concatenates two jsonb values. Concatenating two arrays generates an array containing all the elements of each input.
Concatenating two objects generates an object containing the union of their keys, taking the second object's value when there
are duplicate keys
Deleting fields
jsonb - text → jsonb
Deletes a key (and its value) from a JSON object, or matching string value(s) from a JSON array.
Deleting fields
jsonb - text[] → jsonb
Deletes all matching keys or array elements from the left operand.
Returns target with the item designated by path replaced by new_value, or with new_value added if create_if_missing is
true (which is the default) and the item designated by path does not exist
Migration example