2016-04-09 8 views
0

上の複雑なJMESPathフィルタ以下のJSONエキスを検討してください(データははるかに大きいが、これは私が仕事に取得しようとしていることの短い作品です)大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番号この方法を失います。

結果:

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

任意のアイデアはどのようにその結果を取得する:

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

は私が取得したいのですか?

答えて

1

まあ私はこれについての答えを探し続け、私は最終的に私の結果に到達することに成功しました!

キーは、2つのステップでこれを行うことでした:)

これは私が今使用してコードです:

#!/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" 
    } 
] 
関連する問題