0% found this document useful (0 votes)
12 views

JSONB - Postgresql

The document discusses PostgreSQL's json and jsonb data types for storing JSON data, with jsonb being faster to process but slightly slower to input due to added conversion overhead and supporting indexing. It provides details on operators like @> and ->> for querying and manipulating JSONB values as well as functions like jsonb_array_elements and jsonb_set for expanding and updating JSONB documents. The document also gives examples for migrating between JSON and JSONB data.

Uploaded by

LZC
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

JSONB - Postgresql

The document discusses PostgreSQL's json and jsonb data types for storing JSON data, with jsonb being faster to process but slightly slower to input due to added conversion overhead and supporting indexing. It provides details on operators like @> and ->> for querying and manipulating JSONB values as well as functions like jsonb_array_elements and jsonb_set for expanding and updating JSONB documents. The document also gives examples for migrating between JSON and JSONB data.

Uploaded by

LZC
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

{ } 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

The major practical difference is one of efficiency.


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.

In general, most applications should prefer to store JSON data as jsonb,


JSONB Containment rules
Containment tests whether one jsonb document has contained within it another one.

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

jsonb @> jsonb → boolean

Does the first JSON value contain the second?


-> operator: Accessing fields in a JSON

json -> integer → json


jsonb -> integer → jsonb

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

json ->> integer → text


jsonb ->> integer → text

Extracts n'th element of JSON array, as text.


->> operator
json ->> text → text
jsonb ->> text → text

Extracts JSON object field with the given key, as text.


JSON subscripting
The jsonb data type supports array-style subscripting expressions to extract and modify elements.
Adding fields
jsonb || jsonb → jsonb

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.

jsonb - integer → jsonb


Deletes the array element with specified index (negative integers count from the end). Throws an error if JSON value is not an
array.
Handy functions for updating JSONBs
jsonb_array_elements:

jsonb_array_elements ( jsonb ) → setof jsonb


Expands the top-level JSON array into a set of JSON values.
select * from json_array_elements('[1,true, [2,false]]')
Handy functions for updating JSONBs
With Ordinality

- You use with ordinality with set-returning functions


- A set returning function is a function that can return more than one row
- What ordinality does is append a bigint column to your table on which the output starts with
1 and increments by 1 for each row
- The main purpose of with ordinality is to retain the original position of each element in
whatever it is you unnested or want aggregated because order is not retained during
aggregation ( this is where ordinality is very handy )

Example: table plus


Handy functions for updating JSONBs
jsonb_set:
jsonb_set ( target jsonb, path text[], new_value jsonb [, create_if_missing boolean ] ) → jsonb

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

You might also like