给出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
的变通方法,但是我觉得必须有一种不错的方法来做到这一点,而我却不见了。
答案 0 :(得分:1)
A() noexcept {}
答案 1 :(得分:1)
您可以简单地写:
jq '.other[.thing[0]]'