请考虑以下 JSON 提取(数据要大得多,但这是我要开始工作的一小部分)
jsonData = """{
"products" : {
"DQ578CGN99KG6ECF" : {
"sku" : "DQ578CGN99KG6ECF",
"productFamily" : "Compute",
"attributes" : {
"location" : "US East (N. Virginia)",
"instanceType" : "hs1.8xlarge",
"tenancy" : "Shared",
"operatingSystem" : "Windows",
"licenseModel" : "License Included",
"preInstalledSw" : "NA"
}
},
"G2N9F3PVUVK8ZTGP" : {
"sku" : "G2N9F3PVUVK8ZTGP",
"productFamily" : "Instance",
"attributes" : {
"location" : "Asia Pacific (Seoul)",
"instanceType" : "i2.xlarge",
"tenancy" : "Host",
"operatingSystem" : "Windows",
"licenseModel" : "License Included",
"preInstalledSw" : "SQL Server Enterprise"
}
},
"FBZZ2TKXWWY5HZRX" : {
"sku" : "FBZZ2TKXWWY5HZRX",
"productFamily" : "Compute",
"attributes" : {
"location" : "Asia Pacific (Seoul)",
"instanceType" : "i2.4xlarge",
"tenancy" : "Dedicated",
"operatingSystem" : "SUSE",
"licenseModel" : "No License required",
"preInstalledSw" : "NA"
}
}
}
}"""
我无法创建适当的过滤器来查找所有使用“Windows”作为操作系统和租户共享的产品。
我到了这一点:
priceJson = json.loads(jsonData)
query = "products.*.attributes[?operatingSystem=='Windows' && tenancy=='Shared']"
output_dict = jmespath.search(query, priceJson)
但是我以这种方式丢失了 sku #。
结果:
[{
"location" : "US East (N. Virginia)",
"instanceType" : "hs1.8xlarge",
"tenancy" : "Shared",
"operatingSystem" : "Windows",
"licenseModel" : "License Included",
"preInstalledSw" : "NA"
}]
我想得到什么:
[
{ "sku": "DQ578CGN99KG6ECF",
"attributes" : {
"location" : "US East (N. Virginia)",
"instanceType" : "hs1.8xlarge",
"tenancy" : "Shared",
"operatingSystem" : "Windows",
"licenseModel" : "License Included",
"preInstalledSw" : "NA"
}
}]
知道如何得到这个结果吗?