json - Getting only desired properties from nested array values with jq -


the structure want be:

{   "catalog": [     {       "name": "x",       "catalog": [         { "name": "y", "uniqueid": "z" },         { "name": "q", "uniqueid": "b" }       ]     }   ] } 

this existing structure looks except there many other properties @ each level (https://gist.github.com/ajcrites/e0e0ca4ca3a08ff2dc401ec872e6094c). want filter out , json format looks this.

i have started out with: jq '.catalog', returns array. still want catalog property name there. can jq '{catalog: .catalog[]}, prints out each catalog object individually makes whole output invalid json. still want properties in array. there way filter specific property key-values within arrays using jq?

the following transforms given input desired output , may want:

{catalog} | .catalog |= map( {name, catalog} ) | .catalog[].catalog |= map( {name, uniqueid} ) | .catalog |= .[0:1] 

however, it's not clear me want, don't discuss duplication in given json input. maybe don't want last line in above, or maybe want duplicates handled in other way, or ....

anyway, trick keeping things simple here use |=.

an alternative approach use del delete unwanted properties (rather selecting ones want), in present case, (at best) tedious.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -