When you want to process a JSON file in Jenkins pipeline, you can do in various ways
-
Pipeline Utility Steps
readJSON
- jq
- JSON Parser in Python, NodeJS. etc
This article will focus on the problem with jq vs Jenkins sh
since it introducing some interesting problem.
Sample Data
file.json
{
"lorem": {
"key": "Lorem Ipsum",
"dash-key": "Lorem Ipsum",
"underscore_key": "Lorem Ipsum",
}
}
In the jq, a syntax to get the JSON value from a key is
$ cat file.json | jq -r .lorem.key
Lorem Ipsum
$ cat file.json | jq -r .lorem.underscore_key
Lorem Ipsum
But, it will have a problem when your JSON key contains "-" dash.
$ cat file.json | jq -r .lorem.dash-key
jq: error: key/0 is not defined at <top-level>, line 1:
.lorem.dash-key
jq: 1 compile error
With some limitation with jq, a key string contains dash -
have to combine pattern with single-quote and double-quote when query. Possible in many pattern to get a value.
Dash Issue with jq Github Issue, Stack Overflow
$cat file.json | jq -r '.lorem."dash-key"'
Lorem Ipsum
$cat file.json | jq -r .lorem.'"dash-key"'
Lorem Ipsum
$cat file.json | jq -r '.lorem["dash-key"]'
Lorem Ipsum
$cat file.json | jq -r .lorem.\"dash-key\"
Lorem Ipsum
When we bring the jq query into Jenkins with a key name as a variable. It will lead to next problem, as Jenkins have some weird way to manipulating string with single-quote and double-quote in sh
function.
Fun read Adventures with escaping quotes in Jenkins and Gist of weird escape on Jenkins
Then, we need to escape double-quote with Jenkins sh
and jq
with this pattern \\\"
SOME_KEY = "dash-key"
value = sh(returnStdout: true, script: "cat file.json | jq -r .lorem.\\\"${SOME_KEY}\\\"").trim()
Top comments (0)