jq icon indicating copy to clipboard operation
jq copied to clipboard

JQ exists in case where it cant find the json key instead of silently continuing

Open agamsol opened this issue 3 years ago • 12 comments

Hello everyone, I am posting this request after some research which I couldn't reproduce Basically I want to parse multiple json keys from file - now the problem is that if one or more keys does not exist, the whole job gets terminated by an error instead of continuing to process the other keys

What could I do against it?

agamsol avatar Jul 07 '22 14:07 agamsol

Could you post an example with input, query, actual and expected output? But sounds like you want ? or try.

wader avatar Jul 07 '22 14:07 wader

Could you post an example with input, query, actual and expected output? But sounds like you want ? or try.

template.txt

"[].commit.sha=\(.[].commit.sha)", "array[1]=\(.array[1])"

Command:


call "%~dp0\jq.exe" -n -f "template.txt" "json-tests\Test-1.1.json"

In this case the key .[].commit.sha doesn't exist in the json file but the key .array[1] does The main problem is that due to the error it doesn't continue checking the rest of the keys

agamsol avatar Jul 07 '22 14:07 agamsol

Something like this?

$ echo '[{"commit": {"sha": "sha"}}]' | jq '"[].commit.sha=\(.[].commit.sha)"?, "array[1]=\(.array[1])"?'
"[].commit.sha=sha"
$ echo '{"array": ["a", "b"]}' | jq '"[].commit.sha=\(.[].commit.sha)"?, "array[1]=\(.array[1])"?'
"array[1]=b"

<expr>? is a short hand for try <expr> which itself is a short hand for try <expr> catch empty

wader avatar Jul 07 '22 14:07 wader

I would like to mention that I am using jq in windows operating system (cmd.exe) I still couldn't understand what to do in order to solve the problem

agamsol avatar Jul 07 '22 14:07 agamsol

I guess change template.txt to:

"[].commit.sha=\(.[].commit.sha)"?, "array[1]=\(.array[1])"?

wader avatar Jul 07 '22 14:07 wader

I guess change template.txt to:

"[].commit.sha=\(.[].commit.sha)"?, "array[1]=\(.array[1])"?

Its fine now, The thing is that if the key doesn't exist it says null is it possible to avoid it from happening and just not output anything in that case?

agamsol avatar Jul 07 '22 14:07 agamsol

A bit cryptic maybe but this probably works:

"[].commit.sha=\(.[].commit.sha | values)"?, "array[1]=\(.array[1] | values)"?

It could probably be made more readable and explicit by moving out the filters from the string interpolation somehow.

wader avatar Jul 07 '22 15:07 wader

A bit cryptic maybe but this probably works:

"[].commit.sha=\(.[].commit.sha | values)"?, "array[1]=\(.array[1] | values)"?

It could probably be made more readable and explicit by moving out the filters from the string interpolation somehow.

Last question, is it possible to invoke the keys without any temporary files? I couldn't do it and make it work

agamsol avatar Jul 07 '22 21:07 agamsol

You can pipe the json on stdin and provide the filter as an argument

wader avatar Jul 08 '22 09:07 wader

I tried many times, could you try and let me know?

agamsol avatar Jul 08 '22 16:07 agamsol

Sorry i'm not a windows user that much and not sure i follow how you invoke jq

wader avatar Jul 12 '22 16:07 wader

Could you post an example with input, query, actual and expected output? But sounds like you want ? or try.

template.txt

"[].commit.sha=\(.[].commit.sha)", "array[1]=\(.array[1])"

Command:

call "%~dp0\jq.exe" -n -f "template.txt" "json-tests\Test-1.1.json"

In this case the key .[].commit.sha doesn't exist in the json file but the key .array[1] does The main problem is that due to the error it doesn't continue checking the rest of the keys

What if I would like jq to output the keys in the way they were provided? I had some look at the docs but still couldn't merge the keys_unsorted/0 key with the file formatted which has all keys?

agamsol avatar Jul 12 '22 21:07 agamsol

@agamsol - If you still have any unresolved support questions, please ask them at stackoverflow.com using the jq tag:

https://stackoverflow.com/questions/tagged/jq

pkoppstein avatar Jun 30 '23 08:06 pkoppstein