NoSQL Technical Information

NoSQL Technical Information

Postgres JSON/JSONB Operators and Functions

The tables below show the wealth of operators and functions available for storing, retrieving, converting, manipulating and creating JSON data in a Postgres relational environment. When these features are used with Postgres' Key-Value format (HSTORE), full text search, trigram search, nearest neighbor search, GIN and GiST indexing, Unlogged Tables, XML support, foreign data wrappers (e.g. MongoDB, CouchDB and Redis), and asynchronous commit settings for 'eventual consistency' you will find you don't need another specialized database to meet your unstructured data needs.

Postgres' JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 7159. The JSON data type stores an exact copy of the input text, which processing functions must reparse on each execution; 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.

 

The table below shows operators that are available for use with the two JSON datatypes: JSON and JSONB.

JSON and JSONB Operators
Operator Right Operand Type Description
–> int Get JSON array element (indexed from zero,negative integers count from the end)
–> text Get JSON object field by key
–>> int Get JSON array element as text
–>> text Get JSON object field as text
#> text[ ] Get JSON object at specified path
#> text[ ] Get JSON object at specified path as text

 

 

Additional operators exist only for JSONB, the binary storage format for JSON.

Additional JSONB Operators
Operator Right Operand Type Description
@> jsonb Does the left JSON value contain the right JSON path/value entries at the top level?
<@ jsonb Are the left JSON path/value entries contained at the top level within the right JSON value?
? text Does the string exist as a top-level key within the JSON value?
?| text[ ] Do any of these array strings exist as top-level keys?
?& text[ ] Do all of these array strings exist as top-level keys?
| | jsonb Concatenate two jsonb values into a new jsonb value.
- text Delete key/value pair or string element from left operand. Key/value pairs are matched based on their key value.
- integer Delete the array element with specified index (Negative integers count from the end). Throws an error if top level container is not an array.
#- text[] Delete the field or element with specified path (for JSON arrays, negative integers count from the end).

 

 

This table shows the functions that are available for creating JSON values. Currently, there are no equivalent functions for JSONB, but you can cast the result of one of these functions to JSON.

JSON Creation Functions
Function Description
to_json(anyelement)

to_jsonb(anyelement)

Returns the value as json or jsonb. Arrays and composites are converted (recursively) to arrays and objects; otherwise, if there is a cast from the type to json, the cast function will be used to perform the conversion; otherwise, a scalar value is produced. For any scalar type other than a number, a Boolean, or a null value, the text representation will be used, in such a fashion that it is a valid json or jsonb value.
array_to_json(anyarray [, pretty_bool]) Returns the array as a JSON array. A PostgreSQL multidimensional array becomes a JSON array of arrays. Line feeds will be added between dimension-1 elements if pretty_bool is true.
row_to_json(record [, pretty_bool]) Returns the row as a JSON object. Line feeds will be added between level-1 elements if pretty_bool is true.
json_build_array(VARIADIC "any")

jsonb_build_array(VARIADIC "any")

Builds a possibly-heterogeneously-typed JSON array out of a variadic argument list.
json_build_object(VARIADIC "any")

jsonb_build_object(VARIADIC "any")

Builds a JSON object out of a variadic argument list. By convention, the argument list consists of alternating keys and values.
json_object(text[ ])

jsonb_object(text[ ])

Builds a JSON object out of a text array. The array must have either exactly one dimension with an even number of members, in which case they are taken as alternating key/value pairs, or two dimensions such that each inner array has exactly two elements, which are taken as a key/value pair.
json_object(keys text[ ], values text[ ])

jsonb_object(keys text[ ], values text[ ])

This form of json_object takes keys and values pairwise from two separate arrays. In all other respects it is identical to the one-argument form.

 

 

The final table shows the functions that are available for processing JSON and JSONB values.

JSON Processing Functions
Function Return Type Description
json_array_length(json)

jsonb_array_length(jsonb)

int Returns the number of elements in the outermost JSON array.
json_each(json)

jsonb_each(jsonb)

setof key text, value json

setof key text, value jsonb

Expands the outermost JSON object into a set of key/value pairs.
json_each_text(json)

jsonb_each_text(jsonb)

setof key text,

value text

Expands the outermost JSON object into a set of key/value pairs. The returned values will be of type text.
json_extract_path(from_json json, VARIADIC path_elems text[ ])

jsonb_extract_path(from_jsonb jsonb, VARIADIC path_elems text[ ])

json

jsonb

Returns JSON value pointed to by path_elems (equivalent to #> operator).
json_extract_path_text(from_json json, VARIADIC path_elems text[ ])

jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[ ])

test Returns JSON value pointed to by path_elems as text (equivalent to #>> operator).
json_object_keys(json)

jsonb_object_keys(jsonb)

setof text Returns set of keys in the outermost JSON object.
json_populate_record(base anyelement, from_json json)

jsonb_populate_record(base anyelement, from_json jsonb)

anyelement Expands the object in from_json to a row whose columns match the record type defined by base (see note below).
json_populate_recordset(base anyelement, from_json json)

jsonb_populate_recordset(base anyelement, from_json jsonb)

setof anyelement Expands the outermost array of objects in from_json to a set of rows whose columns match the record type defined by base (see note below).
json_array_elements(json)

jsonb_array_elements(jsonb)

setof json

setof jsonb

Expands a JSON array to a set of JSON values.
json_array_elements_text(json)

jsonb_array_elements_text(jsonb)

setof text Expands a JSON array to a set of text values.
json_typeof(json)

jsonb_typeof(jsonb)

text Returns the type of the outermost JSON value as a text string. Possible types are object, array, string, number, boolean, and null.
json_to_record(json)

jsonb_to_record(jsonb)

record Builds an arbitrary record from a JSON object (see note below). As with all functions returning record, the caller must explicitly define the structure of the record with an AS clause.
json_to_recordset(json)

jsonb_to_recordset(jsonb)

setof record Builds an arbitrary set of records from a JSON array of objects (see note below). As with all functions returning record, the caller must explicitly define the structure of the record with an AS clause.
json_strip_nulls(from_json json)

jsonb_strip_nulls(from_json jsonb)

json

jsonb

Returns from_json with all object fields that have null values omitted. Other null values are untouched.
jsonb_set(target jsonb, path text[], new_value jsonb[, create_missing boolean]) jsonb Returns target with the section designated by path replaced by new_value, or with new_value added if create_missing is true ( default is true) and the item designated by path does not exist. As with the path orientated operators, negative integers that appear in path count from the end of JSON arrays.
jsonb_pretty(from_json jsonb) text Returns from_json as indented JSON text.

 

Click here for the full JSON Functions and Operator Reference with Examples and Example Results.

 

For more information about the JSON capabilities in Postgres...