Sample Header Ad - 728x90

Add key and value to elements of a jsonb array where the key does not exist yet

1 vote
1 answer
7584 views
I have a table tbl with the following structure: ~~~pgsql CREATE TABLE tbl ( org text , data jsonb ); ~~~ The data in the jsonb field is an array structured in the following way: ~~~pgsql INSERT INTO tbl VALUES ('SOMETHING' , '[ { "type": "XYZ", "valueA": "500", "valueB": "ABC" }, { "type": "ABC", "valueA": "300", "valueB": "CDE" } ]') ; ~~~ I want to add a key valueC to elements of data where the object has a "type" key with a value of "XYZ". valueC's value will be an array of strings. The values of the array will depend on the value of the org column. I want to do this for all rows such that if a specific org is present, and the jsonb array in the data column contains an object with "type": "XYZ", then I get this result:
[
  {
    "type": "XYZ",
    "valueA": "500",
    "valueB": "ABC",
    "valueC": ["SOMETHING"],
  },
  {
    "type": "ABC",
    "valueA": "300",
    "valueB": "CDE",
  }
]
I also want to ensure this script only runs if valueC is not present in the object that matches the conditions, so it is not re-run during a migration/rollback unless needed. Here's what I have so far but it's not working when it does not find a result to the subquery and I can't figure out how to only run this if valueC does not exist: ~~~pgsql UPDATE tbl SET data = jsonb_set( data, '{data}', (SELECT jsonb_agg(elem ||'{"valueC":["SOMETHING"]}') FROM jsonb_array_elements(data->'data') as elem where elem ->> 'type' = 'XYZ') ) WHERE org = 'SOMETHING'; ~~~
Asked by sharknado (23 rep)
Aug 16, 2022, 04:54 PM
Last activity: Aug 17, 2022, 12:28 AM