Postgres: Expand JSON column into rows

我是研究僧i 提交于 2020-12-02 06:59:21

问题


In a Postgres 9.3 database I have a table in which one column contains JSON, as in the test table shown in the example below.

test=# create table things (id serial PRIMARY KEY, details json, other_field text);
CREATE TABLE
test=# \d things
                            Table "public.things"
   Column    |  Type   |                      Modifiers
-------------+---------+-----------------------------------------------------
 id          | integer | not null default nextval('things_id_seq'::regclass)
 details     | json    |
 other_field | text    |
Indexes:
    "things_pkey" PRIMARY KEY, btree (id)

test=# insert into things (details, other_field) 
       values ('[{"json1": 123, "json2": 456},{"json1": 124, "json2": 457}]', 'nonsense');
INSERT 0 1
test=# insert into things (details, other_field) 
       values ('[{"json1": 234, "json2": 567}]', 'piffle');
INSERT 0 1
test=# select * from things;
 id |                           details                           | other_field
----+-------------------------------------------------------------+-------------
  1 | [{"json1": 123, "json2": 456},{"json1": 124, "json2": 457}] | nonsense
  2 | [{"json1": 234, "json2": 567}]                              | piffle
(2 rows)

The JSON is always an array containing a variable number of hashes. Each hash always has the same set of keys. I am trying to write a query which returns a row for each entry in the JSON array, with columns for each hash key and the id from the things table. I'm hoping for output like the following:

 thing_id | json1 | json2
----------+-------+-------
        1 |   123 |   456
        1 |   124 |   457
        2 |   234 |   567

i.e. two rows for entries with two items in the JSON array. Is it possible to get Postgres to do this? json_populate_recordset feels like an essential part of the answer, but I can't get it to work with more than one row at once.


回答1:


select id,
    (details ->> 'json1')::int as json1,
    (details ->> 'json2')::int as json2
from (
    select id, json_array_elements(details) as details
    from things
) s
;
 id | json1 | json2 
----+-------+-------
  1 |   123 |   456
  1 |   124 |   457
  2 |   234 |   567


来源:https://stackoverflow.com/questions/25978846/postgres-expand-json-column-into-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!