16

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.

4
  • 2
    your description is ambiguous. What does mean your if value this keys is null? Update your question with clear description, avoid confusing phrases May 15, 2018 at 9:43
  • 1
    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?
    – JigglyNaga
    May 15, 2018 at 12:30
  • 4
    Please don't overwork your cats - just open the file directly to stdin (i.e. <jsonfile). Thank you; end of public service announcement. May 15, 2018 at 14:41
  • 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
    May 19, 2018 at 4:08

1 Answer 1

25

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
}
3
  • thanks, @JigglyNaga, how to check value these keys are zero? if key11 or key12 or key13 value is 0 I get to know.
    – pyramid13
    May 19, 2018 at 5:37
  • @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.
    – JigglyNaga
    May 20, 2018 at 9:56
  • My question has changed :) . please look this link qustion . thanks @JigglyNaga.
    – pyramid13
    May 20, 2018 at 10:17

Not the answer you're looking for? Browse other questions tagged .