2016-04-09 3 views
0

Пожалуйста, обратите внимание на следующий отрывок в формате JSON (данные гораздо больше, но это более короткая часть этого я пытаюсь работать)комплекс JMESPath фильтр на большом 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) 

однако я потерять СКУ # этот путь.

Результат:

[{   
     "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" 
    } 
}] 

Любая идея, как добраться до этого результата?

ответ

1

Ну, я продолжал искать ответ на это, и мне, наконец, удалось добраться до моего результата!

Ключ в том, чтобы сделать это в двух шагах :)

Это код, который я использую сейчас:

#!/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" 
    } 
] 
Смежные вопросы