[英]extract value from subarray and combine with upper value using jq to csv
我有一個帶有子數組的 json 文件,我想在不同的行中獲取具有相同 ID 的每個子數組。 json:
{
"success": true,
"status": {
"http": {
"code": 200,
"message": "OK"
}
},
"result": [{
"id": "123456789",
"start_date": "2021-01-01 08:17:39.989",
"snippets": [{
"transcript": "yes",
"matched_entry": null,
"start": "2021-01-16 11:32:25.922"
}, {
"transcript": null,
"matched_entry": null,
"start": "2021-01-16 11:32:38.179"
}]
}, {
"id": "987654321",
"start_date": "2021-01-01 08:17:39.989",
"duration_total": 301,
"snippets": [{
"transcript": "yes",
"matched_entry": null,
"start": "2021-01-01 08:17:54.055"
}, {
"transcript": "something",
"matched_entry": " meta entry",
"start": "2021-01-01 08:18:11.028"
}, {
"transcript": "no",
"matched_entry": null,
"start": "2021-01-01 08:18:24.057"
}]
}]
}
我試圖得到:
123456789, yes , null, "2021-01-16 11:32:25.922"
123456789, null, null, "2021-01-16 11:32:38.179"
987654321, yes, null, "2021-01-01 08:17:54.055"
987654321, something, "meta entry", "2021-01-01 08:18:11.028"
987654321, no, null, "2021-01-01 08:18:24.057"
第一次嘗試是:
jq -rc ".result[] | {id: .id, snippetsTranscript: .snippets[].transcript, snippetsMatchedEntry: .snippets[].matched_entry, snippetsStart: .snippets[].start}" 210101_210121_copy.json
但結果是返回了每個組合:
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"} ...
第二次嘗試是:
jq -rc ".result[] | {id: .id, snippetsMatchedEntry: [.snippets[].matched_entry], snippetsStart: [.snippets[].start], snippetsTranscript: [.snippets[].transcript]}" 210901_210921_copy.json
但結果是:
{"id":"123456789","snippetsMatchedEntry":[null,null],"snippetsStart":["2021-01-16 11:32:25.922","2021-01-16 11:32:38.179"],"snippetsTranscript":["yes",null]}
{"id":"987654321","snippetsMatchedEntry":[null," meta entry",null],"snippetsStart":["2021-01-01 08:17:54.055","2021-01-01 08:18:11.028","2021-01-01 08:18:24.057"],"snippetsTranscript":["yes","something","no"]}
jq可以嗎?
對於result
數組的每個元素,創建一個僅包含id
字段的 object,並為snippets
子數組的每個元素添加它:
.result[] | {id} + .snippets[]
如果您不需要snippets
數組的所有字段,只需像以前一樣聲明它們
.result[] | {id} + (.snippets[] | {transcipt, matched_entry, start})
在jqplay.org上試試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.