Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 1.75 KB

Verifying existence of a root level element with jq.md

File metadata and controls

31 lines (25 loc) · 1.75 KB

(jump to the answer)

Given a JSON structure such as below, I want to test for the existence of the sys/ top-level key.

{                                                                                                                                                             
  "abc/": {                                                                                                                                             
    "uuid": "fd"
   },
 "sys/": {                                                                                                                                                   
    "uuid": "id2"
  }
}

I have tried various constructs such as jq "sys/" or jq "sys/.uuid" but I get compile errors and/or errors such as "jq: error: sys/0 is not defined at , line 1:"

Note that it is important that the result code ($?) from jq returns correctly to reflect actual result (becuse this is being used in a config management test).

A:

jtc does not let setting an arbitrary exit code (it uses own codes to indicate successful or unsuccessful ending result of operation), however, it's possible to do relying on standard unix/linux tools:

bash $ if [ -z "$(jtc -w'[sys/]' file.json)" ]; then (exit 1); else (exit 0); fi
bash $ echo $?
0
bash $ if [ -z "$(jtc -w'[blah]' file.json)" ]; then (exit 1); else (exit 0); fi
bash $ echo $?
1
bash $