在unix中使用jq工具将多级json解析为csv

g6baxovj  于 2023-11-20  发布在  Unix
关注(0)|答案(3)|浏览(170)

我有一个多层次的JSON,如下所示:

  1. {
  2. "id": "id123",
  3. "details": {
  4. "prod": "prod123",
  5. "etype": "type1"
  6. },
  7. "accounts": [
  8. {
  9. "bankName": "bank123",
  10. "accountType": "account123",
  11. "openingBalance": "bal123",
  12. "fromDate": "2023-01-01",
  13. "toDate": "2023-01-01",
  14. "missingMonths": [],
  15. "transactions": [
  16. {
  17. "dateTime": "2020-12-01",
  18. "description": "a very long string",
  19. "amount": -599.0,
  20. "bal": 8154.83,
  21. "type": "Debit"
  22. },
  23. {
  24. "dateTime": "2020-12-01",
  25. "description": "a very long string; a very long string",
  26. "amount": -4000.0,
  27. "balanceAfterTransaction": 4154.83,
  28. "type": "Debit"
  29. }
  30. ]
  31. }
  32. ],
  33. "accountid": "sample123"
  34. }

字符串
我想将其解析为以下格式的csv:

  1. id,prod,etype,bankName,accountType,openingBalance,fromDate,toDate,dateTime,description,amount,bal,type,accountid
  2. id123,prod123,type1,bank123,account123,bal123,2023-01-01,2023-01-01,2020-12-01,a very long string,-599.0,8154.83,Debit,sample123
  3. id123,prod123,type1,bank123,account123,bal123,2023-01-01,2023-01-01,2020-12-01,a very long string; a very long string,-4000.0,4157.83,Debit,sample123


将事务解析为适当的csv,并重复每个事务的公共信息。
到目前为止,我只得到了这么多:
第一个月

6ju8rftf

6ju8rftf1#

这将帮助您获得所需的CSV格式:

  1. jq -r '.[] | "\(.id),\(.details.prod),\(.details.etype),\(.accounts[].bankName)"'

字符串
你可以在它的基础上进一步构建以获得所有想要的值。
注意:对于有效的CSV,您可能需要在某些值(如字符串)周围添加双引号
要解析多个JSON条目,它们应该在一个数组中:

  1. [
  2. {
  3. "id": "id123",
  4. "details": {
  5. "prod": "prod123",
  6. "etype": "type1"
  7. },
  8. "accounts": [
  9. {
  10. .....
  11. }
  12. ]
  13. },
  14. {
  15. "id": "id124",
  16. "details": {
  17. "prod": "prod124",
  18. "etype": "type1"
  19. },
  20. "accounts": [
  21. {
  22. .....
  23. }
  24. ]
  25. }
  26. ]

展开查看全部
sdnqo3pr

sdnqo3pr2#

您可以将外部数据值保存在变量中,并逐步向下到叶节点:

  1. jq -r '"id,prod,etype,bankName,accountType,openingBalance,fromDate,toDate,dateTime,description,amount,bal,type,accountid",
  2. ( . as {$id,$accountid}
  3. | .details as {$prod, $etype}
  4. | .accounts[]
  5. | . as { $bankName,$accountType,$openingBalance,$fromDate,$toDate }
  6. | .transactions[]
  7. | [$id,$prod,$etype,$bankName,$accountType,$openingBalance,$fromDate,$toDate,.dateTime,.description,.amount,.bal? // .balanceAfterTransaction,.type,$accountid]
  8. | join(","))' input.json

字符串

sdnqo3pr

sdnqo3pr3#

下面的解决方案使用了jq的@csv过滤器来提高健壮性,并通过使用也可以用于生成标题行的键值数组来避免冗余。fiddle with .bal使用了jq的//=赋值操作符,以避免干扰程序的主要功能。

  1. ["id","prod","etype"] as $idHdrs
  2. | ["bankName", "accountType", "openingBalance", "fromDate", "toDate"] as $accountHdrs
  3. | ["dateTime", "description", "amount", "bal", "type"] as $txHdrs
  4. | $idHdrs + $accountHdrs + $txHdrs + ["accountid"], # the header row
  5. ( . as {$id,$accountid}
  6. | .details as {$prod, $etype}
  7. | .accounts[]
  8. | [.[$accountHdrs[]]] as $accounts
  9. | .transactions[]
  10. | .bal //= .balanceAfterTransaction # fiddle
  11. | [.[$txHdrs[]]] as $transactions
  12. | [$id,$prod,$etype] + $accounts + $transactions + [$accountid] )
  13. | @csv

字符串
输出量:

  1. "id","prod","etype","bankName","accountType","openingBalance","fromDate","toDate","dateTime","description","amount","bal","type","accountid"
  2. "id123","prod123","type1","bank123","account123","bal123","2023-01-01","2023-01-01","2020-12-01","a very long string",-599,8154.83,"Debit","sample123"
  3. "id123","prod123","type1","bank123","account123","bal123","2023-01-01","2023-01-01","2020-12-01","a very long string; a very long string",-4000,4154.83,"Debit","sample123"

展开查看全部

相关问题