'jq: error (at <stdin>:1): null (null) cannot be matched, as it is not a string
$ jq '.y | gsub(","; "-")' <<< '{"y": "a,b"}'
"a-b"
$ jq '.y | gsub(","; "-")' <<< '{"x": "a"}'
jq: error (at <stdin>:1): null (null) cannot be matched, as it is not a string
I got the above error. I can use if-else-end statement to solve the problem. But it is a little verbose.
What is the most succinct way to fix the error so that .y is still accessed with "," replaced by "-", but when .y does not exist, an empty string will be printed?
Solution 1:[1]
Add the Error Suppression Operator ? which defaults to empty
jq '.y | gsub(","; "-")?'
You may additionally add a default alternative using the Alternative Operator //
jq '.y | gsub(","; "-")? // ""'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pmf |
