Modifying and accessing JSON types

When casting text as a json type, the text is stored and rendered without any processing; so, it will preserve the whitespace, numeric formatting, and element's order details. JSONB does not preserve these details, as shown in the following example:

car_portal=# SELECT '{"name":"some name", "name":"some name" }'::json;
json
------------------------------------------------
{"name":"some name", "name":"some name" }
(1 row)

car_portal=#
car_portal=# SELECT '{"name":"some name", "name":"some name" }'::jsonb;
jsonb
-----------------------
{"name": "some name"}
(1 row)

JSON objects can contain other nested JSON objects, arrays, nested arrays, arrays of JSON objects, and so on. JSON arrays and objects can be nested arbitrarily, allowing the developer to construct complex JSON documents. The array elements in JSON documents can be of different types. The following example shows how to construct an account with name as text value, address as JSON object, and rank as an array:

car_portal=# SELECT '{"name":"John", "Address":{"Street":"Some street", "city":"Some city"}, "rank":[5,3,4,5,2,3,4,5]}'::JSONB;
jsonb
---------------------------------------------------------------------------------------------------------------
{"name": "John", "rank": [5, 3, 4, 5, 2, 3, 4, 5], "Address": {"city": "Some city", "Street": "Some street"}}
(1 row)

One could get the JSON object field as a JSON object or as text. Also, JSON fields can be retrieved using the index or the field name. The following table summarizes the JSON retrieval operators:

Json  Text Description
-> ->> This returns a JSON field either using the field index or field name
#> #>> This returns a JSON field defined by a specified path

 

To get the address and city from the JSON object created before, one could use two methods as follows (note that the field names of JSON objects are case sensitive):

CREATE TABLE json_doc ( doc jsonb );
INSERT INTO json_doc SELECT '{"name":"John", "Address":{"Street":"Some street", "city":"Some city"}, "rank":[5,3,4,5,2,3,4,5]}'::JSONB ;

To return the city from the previous json doc in TEXT format, one can use either the ->> or #>> operators, as follows:

SELECT doc->'Address'->>'city', doc#>>'{Address, city}' FROM json_doc WHERE doc->>'name' = 'John';
?column? | ?column?
-----------+-----------
Some city | Some city
(1 row)

In older version of Postgres, such as 9.4, it is quite difficult to manipulate JSON documents. However, in the newer version, a lot of operators were introduced such as || to concatenate two JSON objects, and - which is used to delete a key-value pair. A simple approach to manipulating a JSON object in older versions of PostgreSQL is to convert it to text then use regular expressions to replace or delete an element, and finally, cast the text to JSON again. To delete the rank from the account object one can do the following:

SELECT (regexp_replace(doc::text, '"rank":(.*)],',''))::jsonb FROM json_doc WHERE doc->>'name' = 'John';

The functions jsonb_set and json_insert were introduced in PostgreSQL 9.5 and PostgreSQL 9.6 respectively. These functions allow us to amend a JSON object to insert into a JSON key-value pair:

car_portal=# update json_doc SET doc = jsonb_insert(doc, '{hobby}','["swim", "read"]', true) RETURNING * ;
doc
-------------------------------------------------------------------------------------------------------------------------------------------

{"name": "John", "rank": [5, 3, 4, 5, 2, 3, 4, 5], "hobby": ["swim", "read"], "Address": {"city": "Some city", "Street": "Some street"}}

To amend an existing key-value pair one can do the following:

car_portal=# update json_doc SET doc = jsonb_set(doc, '{hobby}','["read"]', true) RETURNING * ;
doc
----------------------------------------------------------------------------------------------------------------------------------

{"name": "John", "rank": [5, 3, 4, 5, 2, 3, 4, 5], "hobby": ["read"], "Address": {"city": "Some city", "Street": "Some street"}}

The following code shows how to delete a key-value pair:

car_portal=# update json_doc SET doc = doc -'hobby' RETURNING * ;
doc
---------------------------------------------------------------------------------------------------------------

{"name": "John", "rank": [5, 3, 4, 5, 2, 3, 4, 5], "Address": {"city": "Some city", "Street": "Some street"}}

The full list of JSON functions and operators can be found at: https://www.postgresql.org/docs/current/static/functions-json.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset