JQ 分组或唯一性对嵌套数组无效。

huangapple go评论63阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年2月19日 00:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定