I'm using PostgreSQL 9.6 and I got the following table:
Cars
ID Name Data
1 abc {"color":"red","wheels":4,"my-value":false}
2 def {"color":"blue","wheels":8}
3 xyz {"color":"yellow","my-value":true}
I want to extend the Data JSON object by adding another boolean attribute called "new-attr" which should get the exact value of "my-value" of the JSON field.
for example, if "my-value" in ID 1 is false, then "new-attr" will be false. And if not set, do not add it at all (as in number 2).
I tried the following but it didn't help:
UPDATE cars SET data=jsonb_set(data,'{new-attr}',data->'my-value')
I expect to get the following JSON :
ID Name Data
1 abc {"color":"red","wheels":4,"my-value":false,"new-attr":false} // same value
2 def {"color":"blue","wheels":8} // no change
3 xyz {"color":"yellow","my-value":true,"new-attr":true} // same value true
Any suggestions ?