Match keys with regex in jq
jq
's regular expressions filters (test
,match
,capture
) take raw text as their input. To apply a regex to key names, you'll first have to convert those key names into text.
jq
provides a function to_entries
for that:
cat jsonfile | jq 'to_entries'
output:
[ { "key": "key11", "value": 1010 }, { "key": "key11_yes", "value": 13 }, { "key": "key11_no", "value": 14 }, { "key": "key12", "value": 12120 }, ... ]
This can then feed into a filter that extracts the key, tests it against a regexp, then either outputs the whole entry or omits it:
if (.key|test("key[0-9][0-9]$")) then ( {key: .key, value: .value } ) else empty end
The matching from_entries
function can convert the remaining entries back to their original form, or with_entries
can do it all in one step:
cat jsonfile|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( {key: .key, value: .value } ) else empty end )'
The result is all the pairs from the input whose keys matched the pattern:
{
"key11": 1010,
"key12": 12120,
"key13": 12103
}
Related videos on Youtube
pyramid13
Updated on September 18, 2022Comments
-
pyramid13 over 1 year
I'm writing a script that analyses JSON input (
jsonfile
):{ "key11":1010,"key11_yes":13,"key11_no":14, "key12":12120,"key12_yes":9,"key12_no":25, "key13":12103,"key13_yes":13,"key13_no":20 }
I want to use
jq tools
, and check the values ofkey11
,key12
,key13
like this:cat jsonfile | jq 'key[1-9][1-9]'
I want the pattern to work like
grep
-style regex:cat jsonfile | grep 'key[1-9][1-9]'
If a matching key has the value null, then my script should
exit 0
.Also I need to check the second parameter if
key[1-9]_[this part is null]
(ie. without appended_yes
or_no
), thenexit 0
.-
Admin almost 6 yearsyour description is ambiguous. What does mean your if value this keys is null? Update your question with clear description, avoid confusing phrases
-
Admin almost 6 yearsI've edited your question to explain the basic problem -- you're trying to match keys by pattern/regexp rather than with an exact string. But I can't understand your examples, or the exact result you want, especially the bit about the "second parameter". Please could you include some example inputs and indicate which ones should reach
exit 0
? -
Admin almost 6 yearsPlease don't overwork your cats - just open the file directly to stdin (i.e.
<jsonfile
). Thank you; end of public service announcement. -
Admin almost 6 yearsI want to check do this format
key11
andkey12
andkey13
exists in the json,also is thevalue
of these keyszero
?
-
-
pyramid13 almost 6 yearsthanks, @JigglyNaga, how to check value these keys are zero? if
key11
orkey12
orkey13
value is0
I get to know. -
VocalFan almost 6 years@pyramid13 Are you looking for zero (
0
) or null, as you originally asked? Either way,grep
should be able to match it. Please edit your question to show the exact input you're trying to match, and what you've tried so far. -
pyramid13 almost 6 yearsMy question has changed :) . please look this link qustion . thanks @JigglyNaga.