jq error while parsing json file inside jenkins Shell
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
2 Answers
2
The posted data conforms with the requirements of HJSON, and can be converted to JSON by:
hjson -j
The jq filter includes .filePath
whereas the HJSON only includes a key named "filepath".
.filePath
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
Provided you fixed your JSON file by adding comma to separate the elements tag
, filepath
and hostname
:
tag
filepath
hostname
{
"environment": {
"stage": {
"testing": [
{
"tag": "1.0",
"filepath": "C:/test/conf",
"hostname": "test"
}
]
}
}
}
You can use this jq
command:
jq
jq -r '.environment.stage.testing.filepath' env.json
Notice the keyword filepath
instead of filePath
in your command.
filepath
filePath
Put a
{
before "environment"
and a }
at the end of the file. The command will work then.– oliv
Jun 29 at 9:11
{
"environment"
}
Yeah added the {} before and after environment, that is also not giving success.
– Manigandan Thanigai Arasu
Jun 29 at 9:28
Difficult to see what you changed in your input file. But for sure, if you take the JSON data from this post, the command will work. It is likely a typo in your input data.
– oliv
Jun 29 at 9:33
Yeah edited the input file, for better clarity
– Manigandan Thanigai Arasu
Jun 29 at 9:58
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Already the elements are seperated by comma only. Also that filepath value is corrected , still experiencing the same error
– Manigandan Thanigai Arasu
Jun 29 at 9:08