jq error while parsing json file inside jenkins Shell

I want to parse the value of the filepath from the json. while parsing it via jq and seeing issue with respect to not defined at top level. Please find the json details and the error below:

"environment":{
             "stage":{
                       "testing":[
{
 "tag":"1.0"
  "filepath":"C:/test/conf"
   "hostname":"test"
}]}}

**command Used in Execute shell:**
 jq -r ".'environment.stage.testing[].filePath' env.json

**Error Faced**:
jq: error: environments/0 is not defined at <top-level>, line 1:
environment.stage.testing[].filePath
jq: 1 compile error

TIA

Upvotes: 0

Views: 1425

Answers (2)

peak
peak

Reputation: 116880

  1. The posted data conforms with the requirements of HJSON, and can be converted to JSON by:

    hjson -j

  2. The jq filter includes .filePath whereas the HJSON only includes a key named "filepath".

  3. The filter that corresponds with the posted data is:

    .environment.stage.testing[].filepath

Putting the above together:

$ hjson -j < so-jenkins-shell.hjson |
  jq -r '.environment.stage.testing[].filepath'
C:/test/conf

Upvotes: 2

oliv
oliv

Reputation: 13259

Provided you fixed your JSON file by adding comma to separate the elements tag, filepath and hostname:

{
  "environment": {
    "stage": {
      "testing": [
        {
          "tag": "1.0",
          "filepath": "C:/test/conf",
          "hostname": "test"
        }
      ]
    }
  }
}

You can use this jq command:

jq -r '.environment.stage.testing[].filepath' env.json

Notice the keyword filepath instead of filePath in your command.

Upvotes: 0

Related Questions