jsonpath: it possible to combine results into an object?

148 Views Asked by At

With jq I can use add to combine results. I am looking for the jsonpath equivalent of this:

$ json="{\"nodes\": [{\"code\": \"DE\", \"label\": \"Germany\", \"ignore\": \"i1\"}, {\"code\": \"NL\", \"label\": \"Netherlands\", \"ignore\": \"i2\"}]}"
$ echo $json | jq '[.nodes[] | {(.code): (.label)}] | add'
{
  "DE": "Germany",
  "NL": "Netherlands"
}

Best I can up with is $..nodes.[*].[code,label]. However, this returns:

["DE",
 "Germany",
 "NL",
 "Netherlands"]

I am trying this website: https://jsonpath.com/

1

There are 1 best solutions below

0
On

The equivalent JSONPath expression for the given jq query would be:

$.nodes[*]{"$[code]": "$[label]"}

This will produce the following output:

{
  "DE": "Germany",
  "NL": "Netherlands"
}