JQ exists in case where it cant find the json key instead of silently continuing
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?
Could you post an example with input, query, actual and expected output? But sounds like you want ? or try.
Could you post an example with input, query, actual and expected output? But sounds like you want
?ortry.
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
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
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
I guess change template.txt to:
"[].commit.sha=\(.[].commit.sha)"?, "array[1]=\(.array[1])"?
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?
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.
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
You can pipe the json on stdin and provide the filter as an argument
I tried many times, could you try and let me know?
Sorry i'm not a windows user that much and not sure i follow how you invoke jq
Could you post an example with input, query, actual and expected output? But sounds like you want
?ortry.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.shadoesn'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 - If you still have any unresolved support questions, please ask them at stackoverflow.com using the jq tag:
https://stackoverflow.com/questions/tagged/jq