對應jq~有沒有更好的折疊單物件陣列的方法?和R:嵌套 data.table 到 JSON 如何只折疊特定元素?
我想擺脫“組”陣列
[
{
"id2": "A",
"group": [
{
"data": [
{
"id1": 1,
"group": [
{
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
}
],
"type": "B"
}
],
"type": "C"
}
]
},
{
"id2": "C",
"group": [
{
"data": [
{
"id1": 3,
"group": [
{
"data": [
{
"a": 1,
"b": 1
}
],
"type": "test"
}
],
"type": "B"
}
],
"type": "C"
}
]
}
]
期望的輸出
[{
"id2": "A",
"group": {
"data": [{
"id1": 1,
"group": {
"data": [{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
},
"type": "B"
}],
"type": "C"
}
},
{
"id2": "C",
"group": {
"data": [{
"id1": 3,
"group": {
"data": [{
"a": 1,
"b": 1
}],
"type": "test"
},
"type": "B"
}],
"type": "C"
}
}
]
該行 'walk(if type=="array" and length==1 then .[0] else . end)'還從單個“資料”物件中洗掉了陣列。
uj5u.com熱心網友回復:
我們可以在嵌套層次結構中操作更高一層,并測驗是否"group"是一個鍵,然后相應地更新.group = .group[0]而不是. = .[0]
jq 'walk(if type=="object"
and has("group")
and (.group | type)=="array"
and (.group | length)==1
then .group = .group[0]
else . end)'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/363331.html
