简体   繁体   English

什么 Postgres 查询可以扩展 json 列的数组并将数组中的每个字段作为一行?

[英]What Postgres query can i expand the array of json column and have each field in the array as a row?

[
   {
      "id":"193",
      "duration":0,
      "count":32,
      "date":"2021-11-10T18:55:00+05:30",
      "period_end":"2022-01-11T00:00:00.000Z",
      "started_at":"2021-11-10T18:55:00+05:30",
      "ended_at":"2021-11-10T19:45:00+05:30"
   }
]

I'm trying to have each of those object keys as a row.我正在尝试将这些 object 中的每一个键都排成一行。 can anyone help?谁能帮忙?

I think each as a column would be more usable, but you asked for each as a row, so you could UNION a bunch of queries such as this:我认为每个作为一列会更有用,但你要求每个作为一行,所以你可以 UNION 一堆查询,例如:

    select 1, info ->> 'id'
    from table1
    union
    select 2, info ->> 'duration'
    from table1
    union
    select 3, info ->> 'count'
    from table1
    union
    select 4, info ->> 'date'
    from table1
    union
    select 5, info ->> 'period_end'
    from table1
    union
    select 6, info ->> 'started_at'
    from table1
    union
    select 7, info ->> 'ended_at'
    from table1
    order by 1

Output: Output:

    1   193
    2   0
    3   32
    4   2021-11-10T18:55:00+05:30
    5   2022-01-11T00:00:00.000Z
    6   2021-11-10T18:55:00+05:30
    7   2021-11-10T19:45:00+05:30

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

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