在PostgreSQL中将动态JSON数据字段提取为列

huangapple go评论51阅读模式
英文:

Dynamic json data fields extract into columns in postgres

问题

我想从JSON中提取数据并放入列中。

create table mytable (id integer, data jsonb);

insert into mytable (id, data) values (25, '{"_id": 25, "indicator 1": "yes", "indicator 2": "yes", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "by_whom": "super_admin", "label": "On Hold", "timestamp": 1688643788}, "start": "2023-07-03T22:03:30.948+05:30"}');
insert into mytable (id, data) values (26, '{"_id": 26, "indicator 2": "no", "indicator 1": "yes", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "by_whom": "super_admin", "label": "On Hold", "timestamp": 1688643788}, "start": "2023-07-03T22:03:30.948+05:30"}');

我的数据如下

id data
25 {"_id": 25, "start": "2023-07-03T22:03:30.948+05:30", "indicator 1": "yes", "indicator 2": "yes", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "label": "On Hold", "by_whom": "super_admin", "timestamp": 1688643788}}
26 {"_id": 26, "start": "2023-07-03T22:03:30.948+05:30", "indicator 1": "yes", "indicator 2": "no", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "label": "On Hold", "by_whom": "super_admin", "timestamp": 1688643788}}

数据需要以此格式

id attributename value
25 _id 25
25 start 2023-07-03T22:03:30.948+05:30
25 indicator 1 yes
25 indicator 2 yes
25 label On Hold
26 _id 26
26 start 2023-07-03T22:03:30.948+05:30
26 indicator 1 yes
26 indicator 2 no
26 label On Hold
英文:

I want to extract data from json and put into columns.

create table mytable (id integer,data jsonb);

insert into mytable (id,data) values(25,'{"_id":25,"indicator 1":"yes","indicator 2":"yes","_validation_status":{"uid":"validation_status_on_hold","color":"#0000ff","by_whom":"super_admin","label":"On Hold","timestamp":1688643788},"start":"2023-07-03T22:03:30.948+05:30"}');
insert into mytable (id,data) values(26,'{"_id":26,"indicator 2":"no","indicator 1":"yes","_validation_status":{"uid":"validation_status_on_hold","color":"#0000ff","by_whom":"super_admin","label":"On Hold","timestamp":1688643788},"start":"2023-07-03T22:03:30.948+05:30"}');

My data is as below

id data
25 {"_id": 25, "start": "2023-07-03T22:03:30.948+05:30", "indicator 1": "yes", "indicator 2": "yes", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "label": "On Hold", "by_whom": "super_admin", "timestamp": 1688643788}}
26 {"_id": 26, "start": "2023-07-03T22:03:30.948+05:30", "indicator 1": "yes", "indicator 2": "no", "_validation_status": {"uid": "validation_status_on_hold", "color": "#0000ff", "label": "On Hold", "by_whom": "super_admin", "timestamp": 1688643788}}

Data is require in this format

id attributtename value
25 _id 25
25 start 2023-07-03T22:03:30.948+05:30
25 indicator 1 yes
25 indicator 2 yes
25 label On Hold
26 _id 26
26 start 2023-07-03T22:03:30.948+05:30
26 indicator 1 yes
26 indicator 2 no
26 label On Hold

答案1

得分: 1

使用jsonb_each_text来从JSON中提取键和值

select id, key, value
from mytable,
jsonb_each_text(mytable.data)
where key != '_validation_status'

id|key        |value                        |
--+-----------+-----------------------------+
25|_id        |25                           |
25|start      |2023-07-03T22:03:30.948+05:30|
25|indicator 1|yes                          |
25|indicator 2|yes                          |
26|_id        |26                           |
26|start      |2023-07-03T22:03:30.948+05:30|
26|indicator 1|yes                          |
26|indicator 2|no                           |
英文:

Use jsonb_each_text to extract the key and values from the JSON

select id, key, value
from mytable,
jsonb_each_text(mytable.data)
where key != '_validation_status'

id|key        |value                        |
--+-----------+-----------------------------+
25|_id        |25                           |
25|start      |2023-07-03T22:03:30.948+05:30|
25|indicator 1|yes                          |
25|indicator 2|yes                          |
26|_id        |26                           |
26|start      |2023-07-03T22:03:30.948+05:30|
26|indicator 1|yes                          |
26|indicator 2|no                           |

huangapple
  • 本文由 发表于 2023年7月6日 21:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定