我想將 json 轉換為 csv。
我為此使用工具“jq”
jq
這里json
{
"total": 2040,
"created_at": "2022-01-27T09:50:59 0200",
"project": "my project",
"issues": [
{
"key": "key_1",
"component": "my_component",
"textRange": {
"startLine": 35,
"endLine": 35,
"startOffset": 46,
"endOffset": 84
},
"flows": [],
"status": "OPEN",
"creationDate": "2022-01-24T06:42:58 0200",
"updateDate": "2022-01-24T06:42:58 0200",
"type": "BUG",
"scope": "MAIN"
},
{
"key": "key2",
"component": "my component 2",
"textRange": {
"startLine": 34,
"endLine": 34,
"startOffset": 3,
"endOffset": 52
},
"flows": [
{
"locations": [
{
"component": "some component",
"textRange": {
"startLine": 35,
"endLine": 35,
"startOffset": 3,
"endOffset": 50
},
"msg": "any message"
}
]
},
{
"locations": [
{
"component": "another component",
"textRange": {
"startLine": 36,
"endLine": 36,
"startOffset": 3,
"endOffset": 71
},
"msg": "message custom"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 37,
"endLine": 37,
"startOffset": 3,
"endOffset": 76
},
"msg": "message number 2"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 38,
"endLine": 38,
"startOffset": 3,
"endOffset": 50
},
"msg": "message number 3"
}
]
}
]
},
{
"key": "my_key3",
"component": "my component 3",
"textRange": {
"startLine": 548,
"endLine": 548,
"startOffset": 14,
"endOffset": 15
},
"flows": [],
"status": "OPEN",
"creationDate": "2022-01-21T17:16:06 0200",
"updateDate": "2022-01-21T17:16:06 0200",
"type": "CODE_SMELL",
"scope": "LOCAL"
}
]
}
這里只轉換為 csv 一些欄位:
jq -r '.issues[] | [.key ,.component, .textRange[], .status] | @csv' test.json
結果如下:
"1","my_component",35,35,46,84,"OPEN"
"2","my component 2",34,34,3,52,
好的。
現在我想轉換欄位(陣列) 流。我試試這個:
jq -r '.issues[] | [.key ,.component, .textRange[], .flows[], .status] | @csv' test.json
結果如下:
"1","my_component",35,35,46,84,"OPEN"
jq: error (at test.json:91): object ({"locations...) is not valid in a csv row
如何修復轉換?陣列(位置)中的陣列(流)
結果必須是這樣的(第一行是欄位的名稱):
total,created_at,project,issues,key,component,textRange,startLine,endLine,startOffset,endOffset,status,creationDate,updateDate,type,scope,flows,locations,component,textRange,startLine,endLine,startOffset,endOffset,msg
2040,2022-01-27T09:50:59 0200,my project,,key_1,my_component,,35,35,46,84,OPEN,2022-01-24T06:42:58 0200,2022-01-24T06:42:58 0200,BUG,MAIN,,,,,,,,,
,,,,key2,my component 2,,34,34,3,52,,,,,,,,some component,,35,35,3,50,any message
,,,,,,,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
,,,,my_key3,my component 3,,548,548,14,15,OPEN,2022-01-21T17:16:06 0200,2022-01-21T17:16:06 0200,CODE_SMELL,LOCAL,,,,,,,,,
不僅要顯示值issues,還要顯示欄位的值,這一點很重要:total,created_at,project
uj5u.com熱心網友回復:
.flows[].loacations[]您是否希望將物件添加為單獨的行?
jq -r '.issues[]
| .key as $key | ., .flows[].locations[]
| [$key ,.component, .textRange[], .status]
| @csv
'
"1","my_component",35,35,46,84,"OPEN"
"2","my component 2",34,34,3,52,
"2","some component",35,35,3,50,
"2","another component",36,36,3,71,
"2","Alarm.java",37,37,3,76,
"2","Alarm.java",38,38,3,50,
演示
uj5u.com熱心網友回復:
看來你需要stringify,那么你可以使用tostring比如
jq -r '.issues[]
| [.key ,.component, .textRange[], (.flows[].locations[]|tostring), .status]
| @csv'
Demo
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422890.html
標籤:
