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
Post a Comment