Postgres 更新 JSON 字段
Postgres update JSON field
我有几个包含如下数据的 Postgres 9.4 表:
| id | data |
|----|-------------------------------------------|
| 1 | {"user": "joe", "updated-time": 123} |
| 2 | {"message": "hi", "updated-time": 321} |
我需要将 JSON 列转换成这样的内容
| id | data |
|----|--------------------------------------------------------------|
| 1 | {"user": "joe", "updated-time": {123, "unit":"millis"}} |
| 2 | {"message": "hi", "updated-time": {321, "unit":"millis"}} |
理想情况下,将转换应用于多个表会很容易。应更新包含 JSON 键 data->'updated-time'
的表,应跳过不包含的表。谢谢!
您可以使用 ||
运算符将两个 jsonb 对象合并在一起。
select '{"foo":"bar"}'::jsonb || '{"baz":"bar"}'::jsonb;
= {"baz": "bar", "foo": "bar"}
我有几个包含如下数据的 Postgres 9.4 表:
| id | data | |----|-------------------------------------------| | 1 | {"user": "joe", "updated-time": 123} | | 2 | {"message": "hi", "updated-time": 321} |
我需要将 JSON 列转换成这样的内容
| id | data | |----|--------------------------------------------------------------| | 1 | {"user": "joe", "updated-time": {123, "unit":"millis"}} | | 2 | {"message": "hi", "updated-time": {321, "unit":"millis"}} |
理想情况下,将转换应用于多个表会很容易。应更新包含 JSON 键 data->'updated-time'
的表,应跳过不包含的表。谢谢!
您可以使用 ||
运算符将两个 jsonb 对象合并在一起。
select '{"foo":"bar"}'::jsonb || '{"baz":"bar"}'::jsonb;
= {"baz": "bar", "foo": "bar"}