Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

For versions less than 9.5 see this question

I have created a table in PostgreSQL using this:

CREATE TEMP TABLE jsontesting AS   SELECT id, jsondata::jsonb FROM ( VALUES     (1, '["abra","value","mango", "apple", "sample"]'),     (2, '["japan","china","india", "russia", "australia"]'),     (3, '["must", "match"]'),     (4, '["abra","value","true", "apple", "sample"]'),     (5, '["abra","false","mango", "apple", "sample"]'),     (6, '["string","value","mango", "apple", "sample"]'),     (7, '["must", "watch"]')   ) AS t(id,jsondata); 

Now what I wanted was to

  • add Something like append_to_json_array takes in the actual jsondata which is a json-array and the newString which I have to add to that jsondata array and this function should return the updated json-array.

    UPDATE jsontesting SET jsondata=append_to_json_array(jsondata, 'newString') WHERE id = 7; 
  • remove a value from the json data array, one function for removing the value.

I tried to search documentation of PostgreSQL but found nothing there.

like image 647
NO WAR WITH RUSSIA Avatar asked Feb 14 '17 18:02

NO WAR WITH RUSSIA


People also ask

Does Postgres compress JSON?

ZSON is a PostgreSQL extension for transparent JSONB compression. Compression is based on a shared dictionary of strings most frequently used in specific JSONB documents (not only keys, but also values, array elements, etc). In some cases ZSON can save half of your disk space and give you about 10% more TPS.

What is the difference between JSON and Jsonb in Postgres?

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.

What is Jsonb in Postgres?

JSONB stands for “JSON Binary” or “JSON better” depending on whom you ask. It is a decomposed binary format to store JSON. JSONB supports indexing the JSON data, and is very efficient at parsing and querying the JSON data. In most cases, when you work with JSON in PostgreSQL, you should be using JSONB.


2 Answers

To add the value use the JSON array append opperator (||)

UPDATE jsontesting SET jsondata = jsondata || '["newString"]'::jsonb WHERE id = 7; 

Removing the value looks like this

UPDATE jsontesting SET jsondata = jsondata - "newString" WHERE id = 7;  

Concatenating to a nested field looks like this

UPDATE jsontesting SET jsondata = jsonb_set(   jsondata::jsonb,   array['nestedfield'],   (jsondata->'nestedfield')::jsonb || '["newString"]'::jsonb)  WHERE id = 7; 
like image 181
NO WAR WITH RUSSIA Avatar answered Sep 21 '22 15:09

NO WAR WITH RUSSIA


To add to Evan Carroll's answer, you may want to do the following to set the column to an empty array if it is NULL. The append operator (||) does nothing if the column is currently NULL.

UPDATE jsontesting SET jsondata = (     CASE         WHEN jsondata IS NULL THEN '[]'::JSONB         ELSE jsondata     END ) || '["newString"]'::JSONB WHERE id = 7; 
like image 43
winduptoy Avatar answered Sep 19 '22 15:09

winduptoy