Skip to main content
 首页 » 编程设计

python之大型 JSON 文件上的复杂 JMESPath 过滤器

2025年04月02日18lyhabc

请考虑以下 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" 
    } 
}] 

知道如何得到那个结果吗?

请您参考如下方法:

好吧,我继续寻找这个问题的答案,我终于成功地得到了我的结果!

关键是分两步完成:)

这是我现在使用的代码:

#!/usr/bin/env python 
try: 
    # For Python 3.0 and later 
    from urllib.request import urlopen 
except ImportError: 
    # Fall back to Python 2's urllib2 
    from urllib2 import urlopen 
 
import json, jmespath 
 
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" 
      } 
    } 
  } 
}""" 
 
priceJson = json.loads(jsonData) 
 
query = "products.*.{sku: sku, location: attributes.location, instanceType: attributes.instanceType, tenancy: attributes.tenancy, operatingSystem: attributes.operatingSystem, licenseModel: attributes.licenseModel, preInstalledSw: attributes.preInstalledSw}" 
output_dict = jmespath.search(query, priceJson) 
 
query2 = "[?operatingSystem=='Windows' && tenancy=='Shared']" 
output_dict = jmespath.search(query2, output_dict) 
 
print(output_dict) 

结果:

[ 
  { 
    "preInstalledSw": "NA", 
    "location": "US East (N. Virginia)", 
    "sku": "DQ578CGN99KG6ECF", 
    "operatingSystem": "Windows", 
    "tenancy": "Shared", 
    "instanceType": "hs1.8xlarge", 
    "licenseModel": "License Included" 
  } 
]