Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON JQ if without else

I use the following JQ command to filter out the JSON. My requirement is to filter out the JSON message if the expected node is present. Or else, do nothing. Hence, I use if, elif, ....

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

Looks like else is mandatory here. I dont want to do anything inside the else block. Is there anyway, I can ignore else or just print some whitespce inside else.

In the above case, it prints double quotes " " in the result file.

like image 666
user1578872 Avatar asked Apr 29 '15 16:04

user1578872


People also ask

Does jq use JSONPath?

jp is a JSON processor for the command line using JSONPath (aka "a simpler jq, and with JSONPath").

How do you pass variables to jq?

Using JSON Variables in jq If you want to use variables that reference JSON objects, this can be done with the --argsjson option. Using --argjson var object will set the variable $var to object . In the example below we set the $location variable to a JSON object, and nest this object into our results.

Is jq fast?

jq is very fast for coding simple, stateless pipelines. Any particular section can be tested by specifying the json value on stdin and looking at the json value on stdout.

What jq command does?

jq is a command-line utility that can slice, filter, and transform the components of a JSON file.


2 Answers

You may want to use the idiom:

if CONDITION then WHATEVER else empty end 

empty is a filter that outputs nothing at all -- not even null, which is after all something (namely a JSON value). It's a bit like a black hole, only blacker -- it will consume whatever it's offered, but unlike a black hole, it does not even emit Hawking radiation.

In your case, you have an "elif" so using "else empty" is probably what you want, but for reference, the above is exactly equivalent to:

select(CONDITION) | WHATEVER 

P.S. My guess is that whatever the goal of the sed command, it could be done more reliably as part of the jq program, perhaps using walk/1.

UPDATE

After the release of jq 1.6, a change was made so that "if without else" has the semantics of "if _ then _ else . end", that is:

if P then Q end === if P then Q else . end

like image 149
peak Avatar answered Sep 16 '22 12:09

peak


Since you're not making any further changes to the object, just use the "identity" filter ..

if (.requests | length) then ... else . end 

On the other hand, you're just updating the requests or result property if non-empty using map. The check isn't necessary. If it's empty, then it will return empty.

You can simplify your filter to just:

.requests |= map(select(.id == \"123\")) | .result |= map(select(.id== \"123\")) 
like image 24
Jeff Mercado Avatar answered Sep 17 '22 12:09

Jeff Mercado