【问题标题】:Issue with jq when using in jenkins pipeline在 jenkins 管道中使用时出现 jq 问题
【发布时间】:2018-06-19 17:20:08
【问题描述】:

我有一个 JSON 对象 x 和一个变量 requiredValue

let requiredValue = 5;

let x = [
{"score":1},
{"score":2},
{"score":3}
}

这里首先使用 jq 我想提取所有的分值,然后检查对象中是否有任何分值大于或等于 requiredValue。

这是我尝试过的

jq -r '.[].score | join(",") | contains([requiredValue])'

假设如果 requiredValue 是 5 那么 jq 查询应该返回 false,如果 requiredValue 是 2 它应该返回 true。

【问题讨论】:

  • lets 不是 JSON 语法,所以 jq 无法处理它们; {[...],[...],[...]} 在我看来也不是有效的 JSON。 {"requiredValue": 5, "x": [{"score": 1},{"score":2},{"score":3}]} 会是它可以处理的东西。
  • @CharlesDuffy 是的,你是对的。现在我已经纠正了。我这里贴错了
  • 即使经过编辑,它仍然是 JavaScript,而不是 JSON。 JSON 中不能有 lets。

标签: bash shell jq


【解决方案1】:

如果您将输入拆分为两个有效的 JSON 文档,而不是让 JavaScript 输入 不是有效的 JSON,您可以执行以下操作:

requiredValue=5
x='
[{"score":1},
 {"score":2},
 {"score":3}]
'

jq -n \
   --argjson requiredValue "$requiredValue" \
   --argjson x "$x" '
  [$x[].score | select(. == $requiredValue)] | any
'

【讨论】:

    【解决方案2】:

    以下内容已经用 bash 测试过:

    requiredValue=5
    
    x='[
    {"score":1},
    {"score":2},
    {"score":3}
    ]'
    
    jq --argjson requiredValue $requiredValue '
      any(.[].score; . >= $requiredValue)' <<< "$x"
    

    结果:

    false
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 2019-12-06
      • 2018-03-13
      • 1970-01-01
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-23
      相关资源
      最近更新 更多