Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jq select elements with keys containing some string, key preserved in results

Tags:

json

select

key

jq

I have a JSON content as embedded in this link jq-play. The JSON content is large and couldn't be accommodated here.

Currently, I manage to get the values by

[.[keys[] | select(contains("VMIMAGE"))]]

but the key names, i.e. CP-COMPUTEENGINE-VMIMAGE-F1-MICRO aren't present in the result. How do I get it?

like image 518
Mzq Avatar asked Aug 16 '18 04:08

Mzq


1 Answers

It looks like you want to take a "slice" of the object by selecting just those keys containing a certain string. Using your query as a model, this can most easily be accomplished using a query of the form with_entries( select(...) ), e.g.:

.gcp_price_list
| with_entries( select(.key|contains("VMIMAGE")))
like image 187
peak Avatar answered Nov 19 '22 07:11

peak