给定JSON如下:
{"thing": ["hello", "bye"], "other": {"hello": "myval"}}我想要一个jq表达式,它在"thing“下获取数组的第一个元素,并在"other”中查找它。所以我就这么做了:
jq '.thing[0] as $tofind | .other.$tofind'但我得到了
jq: error: syntax error, unexpected '$', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.thing[0] as $tofind | .other.$tofind
jq: 1 compile error我尝试过很多其他的方法:
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other.($tofind)'
jq: error: syntax error, unexpected '(', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.thing[0] as $tofind | .other.($tofind)
jq: 1 compile error
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other.[($tofind)]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.thing[0] as $tofind | .other.[($tofind)]
jq: 1 compile error
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other.[$tofind]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.thing[0] as $tofind | .other.[$tofind]
jq: 1 compile error
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other.["$tofind"]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.thing[0] as $tofind | .other.["$tofind"]
jq: 1 compile error
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other."$tofind"'
null
echo '{"thing": ["hello", "bye"], "other": {"hello": "myval"}}' | jq '.thing[0] as $tofind | .other."($tofind)"'
null我在select和to_entries上找到了一种老套的解决方法,但我觉得必须有一种像样的方法来解决我所缺少的问题。
发布于 2019-12-18 03:37:35
.thing[0] as $tofind | .other[$tofind]发布于 2019-12-18 06:44:46
你可以简单地写下:
jq '.other[.thing[0]]'https://stackoverflow.com/questions/59380424
复制相似问题