英文:
JQ Group_by or Unique not working for nested arrays
问题
尝试从嵌套的JSON数组中获取唯一的条目。
JSON源代码
[
{"SIR":[{"PMD":[{"MMA":[{"DN":"One","SA":"1"},{"DN":"Two","SA":"2"}]}]}]},
{"SIR":[{"PMD":[{"MMA":[{"DN":"Two","SA":"2"},{"DN":"Four","SA":"4"}]}]}]},
{"SIR":[{"PMD":[{"MMA":[{"DN":"Three","SA":"3"},{"DN":"Five","SA":"5"}]}]}]}
]
筛选器产生不唯一的结果:
'.[].SIR.PMD.MMA
| [.DN, .SA]
| @csv'
当前输出(CSV)
One,1
Two,2
Two,2
Three,3
Four,4
Five,5
期望的输出(CSV)
One,1
Two,2
Three,3
Four,4
Five,5
英文:
Trying to get unique entries from nested JSON arrays.
JSON Source
[
{"SIR":[{"PMD":[{"MMA":[{"DN":"One","SA":"1"},{"DN":"Two","SA":"2"}]}]}]},
{"SIR":[{"PMD":[{"MMA":[{"DN":"Two","SA":"2"},{"DN":"Four","SA":"4"}]}]}]},
{"SIR":[{"PMD":[{"MMA":[{"DN":"Three","SA":"3"},{"DN":"Five","SA":"5"}]}]}]}
]
Filter producing non-unique results:
'.[].SIR.PMD.MMA
| [.DN, .SA]
| @csv'
Current Output (CSV
One,1
Two,2
Two,2
Three,3
Four,4
Five,5
Desired Output (CSV)
One,1
Two,2
Three,3
Four,4
Five,5
答案1
得分: 2
将要过滤的项目收集到一个数组中,然后使用`unique`单独比较整个对象,或者在任何过滤组合上使用`unique_by`,例如`.DN`):
```sh
jq -r 'map(.SIR[].PMD[].MMA[]) | unique[] | [.DN, .SA] | @csv'
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by(.DN)[] | [.DN, .SA] | @csv'
请注意,输出将按照在第一种情况下整个对象排序,在后一种情况下按照提供的过滤器排序。要实现样本输出的数字顺序,可以在(.SA|tonumber)
上使用unique_by
,或者先在(.SA|tonumber)
上,然后是.DN
。将转换为数字只影响排序,而不影响输出:
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by(.SA|tonumber)[] | [.DN, .SA] | @csv'
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by([(.SA|tonumber), .DN])[] | [.DN, .SA] | @csv'
"One","1"
"Two","2"
"Three","3"
"Four","4"
"Five","5"
英文:
Collect the items to be filtered into an array, and use either unique
alone to compare the whole objects, or unique_by
on any filter combination, e.g. .DN
):
jq -r 'map(.SIR[].PMD[].MMA[]) | unique[] | [.DN, .SA] | @csv'
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by(.DN)[] | [.DN, .SA] | @csv'
Note that the output will be sorted, in the first case by the whole object, in the latter by the filter provided. To achieve the numeric ordering of your sample output, use unique_by
on either just (.SA|tonumber)
, or first (.SA|tonumber)
then .DN
. The conversion to numbers only affects the sorting, not the output:
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by(.SA|tonumber)[] | [.DN, .SA] | @csv'
jq -r 'map(.SIR[].PMD[].MMA[]) | unique_by([(.SA|tonumber), .DN])[] | [.DN, .SA] | @csv'
"One","1"
"Two","2"
"Three","3"
"Four","4"
"Five","5"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论