DEV Community

meddlesome
meddlesome

Posted on • Edited on

Parsing JSON on Jenkins , a problem with sh

When you want to process a JSON file in Jenkins pipeline, you can do in various ways

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",
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)