无法使用jq解析JSON以将值存储在两个变量中

huangapple go评论51阅读模式
英文:

cannot parse json with jq to store value in two vars

问题

var1 = "59.42028985507246" 和 var2 = "18.823529411764707"
英文:

Team,
half way done but cannot figure out what am I missing. new to jq. searched google too.

json from curl response is below
curl -u ${TOKEN}: $url | jq -r '.projectStatus'

{
  "status": "ERROR",
  "conditions": [
    {
      "status": "OK",
      "metricKey": "new_reliability_rating",
      "comparator": "GT",
      "periodIndex": 1,
      "errorThreshold": "1",
      "actualValue": "1"
    },
    {
      "status": "OK",
      "metricKey": "new_security_rating",
      "comparator": "GT",
      "periodIndex": 1,
      "errorThreshold": "1",
      "actualValue": "1"
    },
    {
      "status": "OK",
      "metricKey": "new_maintainability_rating",
      "comparator": "GT",
      "periodIndex": 1,
      "errorThreshold": "1",
      "actualValue": "1"
    },
    {
      "status": "ERROR",
      "metricKey": "new_coverage",
      "comparator": "LT",
      "periodIndex": 1,
      "errorThreshold": "80",
      "actualValue": "59.42028985507246"
    },
    {
      "status": "ERROR",
      "metricKey": "new_duplicated_lines_density",
      "comparator": "GT",
      "periodIndex": 1,
      "errorThreshold": "3",
      "actualValue": "18.823529411764707"
    }
  ],
  "periods": [],
  "ignoredConditions": false
}
new_reliability_rating
new_security_rating
new_maintainability_rating
new_coverage
new_duplicated_lines_density

but I want to pull actualValue from these two sets and store them in var1 and var2

new_coverage
new_duplicated_lines_density

so I tried as first step below.

curl -u ${TOKEN}: $url | jq -r '.projectStatus'.conditions[] | map(select(.metricKey == "new_coverage"))

error

./curl.sh: syntax error near unexpected token `select'

I want to store the response in two vars.

var1 = "59.42028985507246" and var2 = "18.823529411764707"

答案1

得分: 2

你的单引号使用错误,它们应该包围整个参数。你的 jq 程序还存在一些其他小问题,比如在流式处理/迭代数组时不需要使用 `map`。

然而,以下的 jq 命令应该可以工作:

```shell
json="$(curl ...)"
var1="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_coverage").actualValue')"
var2="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_duplicated_lines_density").actualValue')"

或者,可以在变量中只存储公共部分,而不是完整的 JSON:

conditions="$(curl ... | jq '.projectStatus.conditions[]')"
var1="$(printf '%s' "$conditions" | jq -r 'select(.metricKey=="new_coverage").actualValue')"
var2="$(printf '%s' "$conditions" | jq -r 'select(.metricKey=="new_duplicated_lines_density").actualValue')"

在你的 jq 程序中,可以使用括号和 替代运算符 // 来提供一个备用值:

(.projectStatus.conditions[]|select(.metricKey=="new_coverageX").actualValue)//null
英文:

You have your single quotes wrong, they should enclose the full argument. Ther are some other minor issues with your jq program, e.g. map is not needed, when you stream/iterate the array.

Nevertheless, the following jq command should work:

json="$(curl ...)"
var1="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_coverage").actualValue')"
var2="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_duplicated_lines_density").actualValue')"

Alternatively, instead of storing the full json in a variable, you could store only the common part in the variable:

conditions="$(curl ... | jq '.projectStatus.conditions[]')"
var1="$(printf '%s' "$conditions" | jq -r 'select(.metricKey=="new_coverage").actualValue')"
var2="$(printf '%s' "$conditions" | jq -r 'select(.metricKey=="new_duplicated_lines_density").actualValue')"

To provide a fallback value, use parentheses and the alternative operator // in your jq program:

(.projectStatus.conditions[]|select(.metricKey=="new_coverageX").actualValue)//null

huangapple
  • 本文由 发表于 2023年3月23日 10:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定