BMW BMW - 2 months ago 15
JSON Question

How to make it work with filter in jq

I'd like to filter output from below json file to get all start with "tag_Name"

{
...

"tag_Name_abc": [
"10_1_4_3",
"10_1_6_2",
"10_1_5_3",
"10_1_5_5"
],
"tag_Name_efg": [
"10_1_4_5"
],

...

}


Try something but failed.

$ cat output.json |jq 'map(select(startswith("tag_Name")))'
jq: error (at <stdin>:1466): startswith() requires string inputs

Answer Source

There's plenty of ways you can do this but the simplest way you can do so is to convert that object to entries so you can get access to the keys, then filter the entries by the names you want then back again.

with_entries(select(.key | startswith("tag_Name")))