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

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

cannot parse json with jq to store value in two vars

问题

  1. 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'

  1. {
  2. "status": "ERROR",
  3. "conditions": [
  4. {
  5. "status": "OK",
  6. "metricKey": "new_reliability_rating",
  7. "comparator": "GT",
  8. "periodIndex": 1,
  9. "errorThreshold": "1",
  10. "actualValue": "1"
  11. },
  12. {
  13. "status": "OK",
  14. "metricKey": "new_security_rating",
  15. "comparator": "GT",
  16. "periodIndex": 1,
  17. "errorThreshold": "1",
  18. "actualValue": "1"
  19. },
  20. {
  21. "status": "OK",
  22. "metricKey": "new_maintainability_rating",
  23. "comparator": "GT",
  24. "periodIndex": 1,
  25. "errorThreshold": "1",
  26. "actualValue": "1"
  27. },
  28. {
  29. "status": "ERROR",
  30. "metricKey": "new_coverage",
  31. "comparator": "LT",
  32. "periodIndex": 1,
  33. "errorThreshold": "80",
  34. "actualValue": "59.42028985507246"
  35. },
  36. {
  37. "status": "ERROR",
  38. "metricKey": "new_duplicated_lines_density",
  39. "comparator": "GT",
  40. "periodIndex": 1,
  41. "errorThreshold": "3",
  42. "actualValue": "18.823529411764707"
  43. }
  44. ],
  45. "periods": [],
  46. "ignoredConditions": false
  47. }
  1. new_reliability_rating
  2. new_security_rating
  3. new_maintainability_rating
  4. new_coverage
  5. new_duplicated_lines_density

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

  1. new_coverage
  2. new_duplicated_lines_density

so I tried as first step below.

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

error

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

I want to store the response in two vars.

  1. var1 = "59.42028985507246" and var2 = "18.823529411764707"

答案1

得分: 2

  1. 你的单引号使用错误,它们应该包围整个参数。你的 jq 程序还存在一些其他小问题,比如在流式处理/迭代数组时不需要使用 `map`
  2. 然而,以下的 jq 命令应该可以工作:
  3. ```shell
  4. json="$(curl ...)"
  5. var1="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_coverage").actualValue')"
  6. var2="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_duplicated_lines_density").actualValue')"

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

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

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

  1. (.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:

  1. json="$(curl ...)"
  2. var1="$(printf '%s' "$json" | jq -r '.projectStatus.conditions[]|select(.metricKey=="new_coverage").actualValue')"
  3. 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:

  1. conditions="$(curl ... | jq '.projectStatus.conditions[]')"
  2. var1="$(printf '%s' "$conditions" | jq -r 'select(.metricKey=="new_coverage").actualValue')"
  3. 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:

  1. (.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:

确定