Question

I am trying to update a field value in jsonb column with text value present in another column but getting some syntax errors; not getting any solution.

i am trying to swap values of OutgoingVertexid & IncomingVertexId in below JSONB

'[
      {
         "Edgeid":10,
         "Weight":100,
         "Active":1,
         "OutgoingVertexid":"",
         "IncomingVertexid":"G",
         "EdgeColor":"Black"
      }
   ]
'

so used below code by putting all values OutgoingVertexid & IncomingVertexid value in temp table.

UPDATE temp_table 
        SET 
        owner_property = CASE 
                            WHEN owner_outgoing_edge IS NOT NULL 
                                THEN jsonb_set(owner_property, '{OutgoingVertexid}', '""')
                            ELSE 
                                jsonb_set(owner_property, '{OutgoingVertexid}', ''||owner_incoming_edge::jsonb||'') 
                                END;

but getting below error:

ERROR: path element at position 1 is not an integer:

Thanks in Advance

Was it helpful?

Solution

Your json value is an array, not a single value. So you need to pick the array element you want to change by including the index in the array you pass to jsonb_set()

UPDATE temp_table 
  SET owner_property = CASE 
                        WHEN owner_outgoing_edge IS NOT NULL 
                          THEN jsonb_set(owner_property, '{0, OutgoingVertexid}', '""')
                        ELSE 
                          jsonb_set(owner_property, '{0, OutgoingVertexid}', to_jsonb(owner_incoming_edge)) 
                       END
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top