Add a property to existing json
# help
v
Add a property to existing json
So I have a json field that contains an object of title
Copy code
json
{
  "native":"镇魂街",
  "romaji":"Zhen Hun Jie",
  "english":null,
  "userPreferred":"Zhen Hun Jie"
}
now I want to add a new property to it, it should be
Copy code
json
{
  "native":"镇魂街",
  "romaji":"Zhen Hun Jie",
  "english":null,
  "userPreferred":"Zhen Hun Jie",
  "vietnamese": "Trấn Hồn Nhai 2"
}
This is what I've tried so far, it worked with one row, but when I try to run it on all rows, it return this error
invalid input syntax for type json
Copy code
ps
UPDATE kaguya_anime
SET title = title::JSONB || concat('{"vietnamese":"', "vietnameseTitle", '"}')::JSONB
WHERE "vietnameseTitle" IS NOT NULL
g
Can you elaborate on what you means it works on one row, but not all? What is the difference in you SQL between the two?
v
I solved it, thanks for your helping 🙂
Copy code
ps
UPDATE kaguya_anime
SET title = title::JSONB || jsonb_set(title::JSONB, '{vietnamese}', to_jsonb("vietnameseTitle"))
WHERE "vietnameseTitle" IS NOT NULL
I used
jsonb_set
and luckily it worked