我想迭代 JSON 并从 postgresql 转换成行

I want to Itrate JSON and convert into rows from postgresql

基本上我想迭代 JSON 直到它从 table 开始的长度,但其余值保持不变直到当前 JSON 结束。 我的Table格式是这样的

id line txndate metadata docnumber
363 [{"Id": "0", "Amount": 135000.0, "DetailType": "JournalEntryLineDetail", "Description": "Paid Office Rent Of Office", "JournalEntryLineDetail": {"AccountRef": {"name": "Rent or lease payments", "value": "57"}, "PostingType": "Debit"}}, {"Id": "1", "Amount": 135000.0, "DetailType": "JournalEntryLineDetail", "Description": "Paid Office Rent Of Office", "JournalEntryLineDetail": {"AccountRef": {"name": "Cash and cash equivalents:Bank", "value": "83"}, "PostingType": "Credit"}}] 2021-08-16 00:00:00.000000 +00:00 {"CreateTime": "2021-08-20T05:39:38.000000Z", "LastUpdatedTime": "2021-08-20T05:39:38.000000Z"} 332
610 [{"Id": "0", "Amount": 4138088.25, "DetailType": "JournalEntryLineDetail", "Description": "Deposit in Bank", "JournalEntryLineDetail": {"AccountRef": {"name": "Cash and cash equivalents:Bank", "value": "83"}, "PostingType": "Debit"}}, {"Id": "1", "Amount": 4138088.25, "DetailType": "JournalEntryLineDetail", "Description": "Deposit in Bank", "JournalEntryLineDetail": {"AccountRef": {"name": "Share capital", "value": "8"}, "PostingType": "Credit"}}, {"Id": "2", "DetailType": "DescriptionOnly", "Description": "Deposit in Bank"}] 2021-10-11 00:00:00.000000 +00:00 {"CreateTime": "2021-10-13T10:44:09.000000Z", "LastUpdatedTime": "2021-10-13T10:44:09.000000Z"} 560
381 [{"Id": "0", "Amount": 30000.0, "DetailType": "JournalEntryLineDetail", "Description": "Paid to Punkish", "JournalEntryLineDetail": {"AccountRef": {"name": "Advance Against Salary", "value": "103"}, "PostingType": "Debit"}}, {"Id": "1", "Amount": 30000.0, "DetailType": "JournalEntryLineDetail", "Description": "Paid to Punkish", "JournalEntryLineDetail": {"AccountRef": {"name": "Cash and cash equivalents:Bank", "value": "83"}, "PostingType": "Credit"}}] 2021-07-01 00:00:00.000000 +00:00 {"CreateTime": "2021-08-23T05:31:42.000000Z", "LastUpdatedTime": "2021-08-23T05:47:03.000000Z"} 521

但我想提取如下信息table

id line_id Amount Description name value posting_type txndate CreatedTime LastUpdatedTime
363 0 13500 Paid Office Rent Of Office Rent or lease payments 57 Debit 2021-08-16 00:00:00.000000 +00:00 2021-08-20T05:39:38.000000Z 2021-08-20T05:39:38.000000Z
363 1 13500 Paid Office Rent Of Office Cash and cash equivalents:Bank 83 Cebit 2021-08-16 00:00:00.000000 +00:00 2021-08-20T05:39:38.000000Z 2021-08-20T05:39:38.000000Z
610 0 4138088.25 Deposit in Bank Cash and cash equivalents:Bank 83 Debit 2021-10-11 00:00:00.000000 +00:00 2021-10-13T10:44:09.000000Z 2021-10-13T10:44:09.000000Z
610 1 4138088.25 .......... .. ... ... 2021-10-11 00:00:00.000000 +00:00 2021-10-13T10:44:09.000000Z 2021-10-13T10:44:09.000000Z
610 2 4138088.25 .......... .. ... ... 2021-10-11 00:00:00.000000 +00:00 2021-10-13T10:44:09.000000Z 2021-10-13T10:44:09.000000Z
610 3 4138088.25 .......... .. ... ... 2021-10-11 00:00:00.000000 +00:00 2021-10-13T10:44:09.000000Z 2021-10-13T10:44:09.000000Z

我想将 JSON 列条目转换为行,但希望保留 id、txndate、CreatedTime 和 LastUpdatedTime 相同,直到 JSON 列的长度为 line 在我的例子中。

如果可能,请指导我解决方案。 注意:我使用的是 Postgresql,line 列的数据类型是 jsonb

给你,你可以使用jsonb_array_elements函数将JSON的数组转换为行,然后在每一行上查询

Demo

select
  t.id,
  e.value ->> 'Id' as line_id,
  e.value ->> 'Amount' as amount,
  e.value ->> 'Description' as description,
  e.value -> 'JournalEntryLineDetail' -> 'AccountRef' ->> 'name' as name,
  e.value -> 'JournalEntryLineDetail' -> 'AccountRef' ->> 'value' as value,
  e.value -> 'JournalEntryLineDetail' ->> 'PostingType' as posting_type,
  t.txndate,
  t.metadata ->> 'CreateTime' as CreatedTime,
  t.metadata ->> 'LastUpdatedTime' as LastUpdatedTime
from
  test t
  cross join jsonb_array_elements(t.line) e