简体   繁体   English

Postgres更新JSON字段

[英]Postgres update JSON field

I've got several Postgres 9.4 tables that contain data like this: 我有几个Postgres 9.4表,其中包含如下数据:

| id | data                                      |
|----|-------------------------------------------|
| 1  | {"user": "joe", "updated-time": 123}      |
| 2  | {"message": "hi", "updated-time": 321}    |

I need to transform the JSON column into something like this 我需要将JSON列转换成这样

| id | data                                                         |
|----|--------------------------------------------------------------|
| 1  | {"user": "joe", "updated-time": {123, "unit":"millis"}}      |
| 2  | {"message": "hi", "updated-time": {321, "unit":"millis"}}    |

Ideally it would be easy to apply the transformation to multiple tables. 理想情况下,将转换应用于多个表将很容易。 Tables that contain the JSON key data->'updated-time' should be updated, and ones that do not should be skipped. 包含JSON关键字data->'updated-time'应该被更新,而没有的表应被跳过。 Thanks! 谢谢!

You can use the || 您可以使用|| operator to merge two jsonb objects together. 运算符,将两个jsonb对象合并在一起。

select '{"foo":"bar"}'::jsonb || '{"baz":"bar"}'::jsonb;
= {"baz": "bar", "foo": "bar"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM