Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

jq: treat single object as 1-element array

$
0
0

I have several .json files (or rather, .xml files pre-parsed by xq/yq) that I want to parse. Unfortunately, the element I want to read (.size) can be inside an extra array (in which case I'd select one of them) or just a single object, as follows:

{"Obj1": {"size": "123"  },"Obj2": [    { "size": "3" },    { "size": "5" }  ]}

In case of Obj1, the query jq '.[].size' yields "123", as expected, but it fails for Obj2 with the error Cannot index array with string 'size'.In case of Obj2, to get a single size value (e.g. the maximum one), the query jq '.[]|max.size yields "5", as expected. But this fails for Obj1 with the error object {"size":"123"} cannot be iterated over.

Is there an expression that treats the single sub-object {"size":"123"} like an array with one element, or otherwise allow me to treat both cases with the same query?


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>