json - jenkins Shell中解析json文件时出现jq错误

标签 json error-handling compilation runtime-error jq

我想从json解析文件路径的值。同时通过jq对其进行解析,并发现未在顶层定义的问题。请在下面找到json详细信息和错误:

"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

最佳答案

  • 发布的数据符合HJSON的要求,并且可以通过以下方式转换为JSON:

    hjson -j
  • jq筛选器包含.filePath,而HJSON仅包含一个名为“filepath”的键。
  • 与发布的数据相对应的过滤器为:

    .environment.stage.testing []。filepath

  • 将以上内容放在一起:
    $ hjson -j < so-jenkins-shell.hjson |
      jq -r '.environment.stage.testing[].filepath'
    C:/test/conf
    

    关于json - jenkins Shell中解析json文件时出现jq错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097759/

    相关文章:

    php - 如何使用 PHP 和 Mysql 插入马拉地语

    javascript - 从 javascript 中的字符串中删除 "\"

    javascript - 通过 Javascript 将 JSON 文件中的元素添加到 HTML 中

    ios - 在 Swift 的 if 语句中使用自定义参数处理错误

    java - 在 javac 中使用高于 -source 的 -target

    c++ - 什么是Delphi DCU文件?

    javascript - 如何从 Three.js 中的文件中最好地导出/导入 THREE.Vector3 数组?

    c - errno、strerror 和 Linux 系统调用

    javascript - 使用 jQuery 为单选按钮表单创建解释框

    c - 如何处理头文件中的函数定义?