Match keys with regex in jq

26,657

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
}
Share:
26,657

Related videos on Youtube

pyramid13
Author by

pyramid13

Updated on September 18, 2022

Comments

  • pyramid13
    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 of key11, 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), then exit 0.

    • Admin
      Admin almost 6 years
      your description is ambiguous. What does mean your if value this keys is null? Update your question with clear description, avoid confusing phrases
    • Admin
      Admin almost 6 years
      I'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
      Admin almost 6 years
      Please don't overwork your cats - just open the file directly to stdin (i.e. <jsonfile). Thank you; end of public service announcement.
    • Admin
      Admin almost 6 years
      I want to check do this format key11 and key12 and key13 exists in the json,also is the value of these keys zero?
  • pyramid13
    pyramid13 almost 6 years
    thanks, @JigglyNaga, how to check value these keys are zero? if key11 or key12 or key13 value is 0 I get to know.
  • VocalFan
    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
    pyramid13 almost 6 years
    My question has changed :) . please look this link qustion . thanks @JigglyNaga.