JQ - 将 json 转换为 csv - "Multi-levels"
JQ - Converting json to csv - "Multi-levels"
我正在尝试通过 jq 将 json 对象转换为 csv。这是 json 结构:
{
"totalCount": 4440,
"data": [
{
"company": {
"name": "My_company_name",
"countryCode": "US",
"portfolioName": "My_portfolio"
},
"eventDate": "2021-11-22T00:00:00",
"newValue": null,
"oldValue": null,
"ruleCode": 704,
"ruleName": "New Accounts",
"summary": "Explanations..."
},
{
"company": {
"name": "My_company_name 2",
"countryCode": "UK",
"portfolioName": "My_portfolio"
},
"eventDate": "2021-10-22T00:00:00",
"newValue": null,
"oldValue": null,
"ruleCode": 701,
"ruleName": "Hello",
"summary": "otherExplanations..."
}
...
]
}
对于“第一级”中的数据,我没问题:
jq -r '.data | map({eventDate, ruleCode, ruleName, summary, oldValue, newValue}) | (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv' input.json > output.csv
但是我想添加例如公司名称和国家代码,但我不知道这样做,因为这个数据王在第二个“级别”。
我会得到这样的东西:
"eventDate","ruleCode","ruleName","summary","oldValue","newValue","companyName", "companyCountryCode"
"2021-11-22T00:00:00",704,"New Accounts","Explanations...",,,"My_company_name", "US"
"2021-11-22T00:00:00",701,"Hello","otherExplanations...",,,"My_company_name 2", "UK"
你能帮帮我吗?
谢谢
如果你不介意的话,我添加了一个 .
来将 top-level 与 sub-level headers 分开来使事情变得更容易(即 company.name
和company.countryCode
):
jq --raw-output '[
"eventDate",
"ruleCode",
"ruleName",
"summary",
"oldValue",
"newValue",
"company.name",
"company.countryCode"
] as $h
| $h, (.data[] | [getpath($h[] / ".")])
| @csv'
"eventDate","ruleCode","ruleName","summary","oldValue","newValue","company.name","company.countryCode"
"2021-11-22T00:00:00",704,"New Accounts","Explanations...",,,"My_company_name","US"
"2021-10-22T00:00:00",701,"Hello","otherExplanations...",,,"My_company_name 2","UK"
我正在尝试通过 jq 将 json 对象转换为 csv。这是 json 结构:
{
"totalCount": 4440,
"data": [
{
"company": {
"name": "My_company_name",
"countryCode": "US",
"portfolioName": "My_portfolio"
},
"eventDate": "2021-11-22T00:00:00",
"newValue": null,
"oldValue": null,
"ruleCode": 704,
"ruleName": "New Accounts",
"summary": "Explanations..."
},
{
"company": {
"name": "My_company_name 2",
"countryCode": "UK",
"portfolioName": "My_portfolio"
},
"eventDate": "2021-10-22T00:00:00",
"newValue": null,
"oldValue": null,
"ruleCode": 701,
"ruleName": "Hello",
"summary": "otherExplanations..."
}
...
]
}
对于“第一级”中的数据,我没问题:
jq -r '.data | map({eventDate, ruleCode, ruleName, summary, oldValue, newValue}) | (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv' input.json > output.csv
但是我想添加例如公司名称和国家代码,但我不知道这样做,因为这个数据王在第二个“级别”。
我会得到这样的东西:
"eventDate","ruleCode","ruleName","summary","oldValue","newValue","companyName", "companyCountryCode"
"2021-11-22T00:00:00",704,"New Accounts","Explanations...",,,"My_company_name", "US"
"2021-11-22T00:00:00",701,"Hello","otherExplanations...",,,"My_company_name 2", "UK"
你能帮帮我吗?
谢谢
如果你不介意的话,我添加了一个 .
来将 top-level 与 sub-level headers 分开来使事情变得更容易(即 company.name
和company.countryCode
):
jq --raw-output '[
"eventDate",
"ruleCode",
"ruleName",
"summary",
"oldValue",
"newValue",
"company.name",
"company.countryCode"
] as $h
| $h, (.data[] | [getpath($h[] / ".")])
| @csv'
"eventDate","ruleCode","ruleName","summary","oldValue","newValue","company.name","company.countryCode"
"2021-11-22T00:00:00",704,"New Accounts","Explanations...",,,"My_company_name","US"
"2021-10-22T00:00:00",701,"Hello","otherExplanations...",,,"My_company_name 2","UK"