JSON, jq, 合并文本 + 变量 + Bash 变量, 参数

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

JSON, jq, merged text + variable + bash variable, arguments

问题

I have translated the provided content for you:

  1. 我有类似的文件/数据:
  2. ```bash
  3. $ echo "[{\"my_var1\":\"valueA\", \"my_var2\": \"valueB\"}]"
  4. {"my_var1":"valueA", "my_var2": "valueB"}]

在我的代码中,我需要获取“my_var2”的值,很容易:

  1. $ echo "[{\"my_var1\":\"valueA\", \"my_var2\": \"valueB\"}]" | \
  2. jq -r ''.[] .my_var2''
  3. # 正确的结果:
  4. valueB

... 但是... 我需要使用bash变量来指定我想要获取的变量:

我不能指定“my_var2”,我必须只使用“var2”:

  1. $ PART="var2"
  2. $ echo "[{\"my_var1\":\"valueA\", \"my_var2\": \"valueB\"}]" | \
  3. jq -r --arg JSONPART ${PART} ''.[] .my_[$JSONPART]''

期望的结果:

  1. valueB
  1. Please note that I've translated the content as requested, including the code portions.
  2. <details>
  3. <summary>英文:</summary>
  4. I have similar file/data:

$ echo "[{&quot;my_var1&quot;:&quot;valueA&quot;, &quot;my_var2&quot;: &quot;valueB&quot;}]"
{"my_var1":"valueA", "my_var2": "valueB"}]

  1. in my code I need to get value of &quot;my_var2&quot;, easy:

$ echo "[{&quot;my_var1&quot;:&quot;valueA&quot;, &quot;my_var2&quot;: &quot;valueB&quot;}]" |
jq -r '.[] .my_var2'

correct result:

valueB

  1. ... but ... I need to use bash variable to specify which variable I would like to get:
  2. I can&#39;t specify &quot;my_var2&quot;, I have to use only &quot;var2&quot;

$ PART="var2"
$ echo "[{&quot;my_var1&quot;:&quot;valueA&quot;, &quot;my_var2&quot;: &quot;valueB&quot;}]" |
jq -r --arg JSONPART ${PART} '.[] .my_[$JSONPART]'

  1. expected result:

valueB

  1. </details>
  2. # 答案1
  3. **得分**: 2
  4. The jq filter `.xyz` can be rewritten as `.[&quot;xyz&quot;]`. Thus:
  5. ```shell
  6. $ PART=&#39;var2&#39;
  7. $ echo &#39;[{&quot;my_var1&quot;:&quot;valueA&quot;, &quot;my_var2&quot;: &quot;valueB&quot;}]&#39; | \
  8. jq -r --arg JSONPART &quot;$PART&quot; &#39;.[][&quot;my_&quot; + $JSONPART]&#39;
  9. valueB

(Don't forget to properly quote your shell parameters when expanding!)

英文:

The jq filter .xyz can be rewritten as .[&quot;xyz&quot;]. Thus:

  1. $ PART=&#39;var2&#39;
  2. $ echo &#39;[{&quot;my_var1&quot;:&quot;valueA&quot;, &quot;my_var2&quot;: &quot;valueB&quot;}]&#39; | \
  3. jq -r --arg JSONPART &quot;$PART&quot; &#39;.[][&quot;my_&quot; + $JSONPART]&#39;
  4. valueB

(Don't forget to properly quote your shell parameters when expanding!)

答案2

得分: -1

This should work:

  1. $ PART="var2"
  2. $ echo "[{\"my_var1\":\"valueA\", \"my_var2\": \"valueB\"}]" | \
  3. jq -r ".[] .my_$PART"
英文:

This should work:

  1. $ PART=&quot;var2&quot;
  2. $ echo &quot;[{\&quot;my_var1\&quot;:\&quot;valueA\&quot;, \&quot;my_var2\&quot;: \&quot;valueB\&quot;}]&quot; | \
  3. jq -r &quot;.[] .my_$PART&quot;

huangapple
  • 本文由 发表于 2023年4月13日 16:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003568.html
匿名

发表评论

匿名网友

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

确定