if statement - JSON JQ if without else -


i use following jq command filter out json. requirement filter out json message if expected node present. or else, nothing. hence, use if, elif, ....

sed -n "s/.*service - //p" $1/response.log* |   jq "if (.requests | length) != 0 .requests |= map(select(.id == \"123\"))       elif (.result | length ) != 0  .result |= map(select(.id== \"123\"))        else " "  end" > ~/result.log 

looks else mandatory here. dont want inside else block. there anyway, can ignore else or print whitespce inside else.

in above case, prints double quotes " " in result file.

you may want use idiom:

if condition whatever else empty end

empty filter outputs nothing @ -- not null, after (namely json value). it's bit black hole, blacker -- consume whatever it's offered, unlike black hole, not emit hawking radiation.

in case, have "elif" using "else empty" want, reference, above equivalent to:

select(condition) | whatever 

p.s. guess whatever goal of sed command, done more reliably part of jq program, perhaps using walk/1.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -