JMESPath可以用于查询json,但返回不再是json:
例如,使用{"a": "foo", "b": "bar", "c": "baz"} JMESPath a搜索"foo"就会产生"foo"。
我如何返回{"a": "foo"}呢?
jq能够做到这一点:
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
{
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
"name": "Stephen Dolan"
}更新:
我使返回{"a": "foo"}工作,但不适用于更复杂的情况,比如来自AWS CLI查找ec2的安全组id和子网id。的JMESPath查询。也就是说,
'Reservations[].Instances[].[InstanceId,NetworkInterfaces[].Groups[].GroupId,NetworkInterfaces[].SubnetId]'工作,但当我试图使它适应'Reservations[].Instances[].{InstanceId: InstanceId, SubnetId: NetworkInterfaces[].Groups[].GroupId,NetworkInterfaces[].SubnetId}'然后它就不再起作用了。得到以下错误:
查询Reservations[].Instances[].{InstanceId: InstanceId,SubnetId: NetworkInterfaces[].Groups[].GroupId,NetworkInterfaces[].SubnetId}:Expecting:冒号,got: flatten:解析列116处的错误,标记"[]“(平面),用于表达式:"Reservations[].Instances[].{InstanceId: InstanceId,SubnetId: NetworkInterfaces[].Groups[]Groups[],Groups[][]}”
这是JMESPath或aws ec2 describe-instances命令的限制吗?
您可以从https://awscli.amazonaws.com/v2/documentation/api/2.0.34/reference/ec2/describe-instances.html或更好地从https://pastebin.com/JaFwn5ZH获得示例输入。
发布于 2022-09-27 22:31:29
见MultiSelect散列。例如,
>>> import jmespath
>>> expression = jmespath.compile('{a: a}')
>>> expression.search({"a": "foo", "b": "bar", "c": "baz"})
{'a': 'foo'}https://stackoverflow.com/questions/73871302
复制相似问题