我有此格式的JSON对象列表
[
{
"uuid": "2e08c71c-56af-4c1d-9677-0aad7bf38536",
"name": "img-s3",
"summary": "",
"retention_name": "month",
"retention_uuid": "beae2f1a-10e9-4b53-b9b1-baf823624ff2",
"expiry": 2678400,
"schedule_name": "daily",
"schedule_uuid": "baca1a64-b506-4789-83c8-653573f2b99f",
"schedule_when": "daily 2am",
"paused": false,
"store_uuid": "d91fd7b6-4c39-4f73-aa44-8ec55f52da65",
"store_name": "default",
"store_plugin": "s3v8",
"store_endpoint": "{\"access_key_id\":\"xxxx\",\"bucket\":\"images-c4\",\"s3_host\":\"s3.amazon.com\",\"secret_access_key\":\"yyy\",\"signature_version\":\"2\",\"skip_ssl_validation\":true}",
"target_uuid": "991c7aa2-cdd3-4f90-bb28-1431c99f8fc8",
"target_name": "sc-asd",
"target_plugin": "mysql",
"target_endpoint": "{\"mysql_database\":\"users\",\"mysql_host\":\"10.10.22.49\",\"mysql_password\":\"password\",\"mysql_port\":\"3306\",\"mysql_user\":\"user\"}",
"agent": "127.0.0.1:3030"
}
...
]
我只需要使用storege_plugin: "s3v8"
获得对象,并且在storage_endpoint
的“引用”值对象中,键signature_version
是2
要找到具有storage_plugin: "s3v8"
和store_endpoing != null
的对象,我正在使用它:
$ command | jq '.[] | select(.store_plugin == "s3v8" and .store_endpoint != null)'
但是我现在遇到的问题是,store_endpoint
的值被引用了,而我只需要根据键的值来获得结果,在这种情况下,就是signature_value
要测试如何取消报价并搜索值,我正在尝试这样做:
$ echo "{\"access_key_id\":\"xxxx\",\"bucket\":\"images-c4\",\"s3_host\":\"s3.amazon.com\",\"secret_access_key\":\"yyy\",\"signature_version\":\"2\",\"skip_ssl_validation\":true}" \
| jq -r 'select(.signature_version == "2")'
{
"access_key_id": "xxxx",
"bucket": "images-c4",
"s3_host": "s3.amazon.com",
"secret_access_key": "yyy",
"signature_version": "2",
"skip_ssl_validation": true
}
但是想知道如何才能“合并”此选项,以便获得具有一组必需键但依赖于引用对象上存储的嵌套键的对象列表。
答案 0 :(得分:1)
您需要在嵌套的JSON字段上使用fromjson
构造,如下所示。 fromjson
在正式文档中记录得不是很好,但是它允许您将JSON文本解析为用于过滤器的值。
.[] | select((.store_plugin == "s3v8" and .store_endpoint != null) and (.store_endpoint | fromjson | .signature_version == "2") )