简体   繁体   中英

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. 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:

    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:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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