打印python中json文件的所有键

时间:2015-11-01 19:47:27

标签: python json key

我有一个文件夹,我有大约20000个JSON文件。我想找出每个JSON的所有唯一键,我想要所有键的联合。但是,我只是陷入了最初的步骤。我能够找到单个JSON文件的密钥。

到目前为止,我编写了以下代码:

from pprint import pprint
import json
json_data=open("/Users/akira/out/1.json")
jdata = json.load(json_data)

for key, value in jdata:
   pprint("Key:")
   pprint(key)

它给我一个错误如下:

Traceback (most recent call last):
 File "/Users/akira/PycharmProjects/csci572/linkedbased.py",     line 8, in <module>
   for key, value in jdata:
 ValueError: need more than 1 value to unpack

我的JSON是一个嵌套的json。请建议我如何获得所有钥匙。

{
"a": "Offer",
"inLanguage": "et",
"availabl": {
    "a": "Place",
    "address": {
        "a": "PostalAddress",
        "name": "Oklahoma"
    }
},
"description": "Smith and Wesson 686 357 magnum 6 inch barrel wood handle great condition shoots great.",
"priceCurrency": "USD",
"geonames_address": [
    {
        "a": "PopulatedPlace",
        "hasIdentifier": {
            "a": "Identifier",
            "label": "4552707",
            "hasType": "http://dig.isi.edu/gazetteer/data/SKOS/IdentifierTypes/GeonamesId"
        },
        "hasPreferredName": {
            "a": "Name",
            "label": "Tahlequah"
        },
        "uri": "http://dig.isi.edu/gazetteer/data/geonames/place/4552707",
        "fallsWithinState1stDiv": {
            "a": "State1stDiv",
            "uri": "http://dig.isi.edu/gazetteer/data/geonames/place/State1stDiv/US_OK",
            "hasName": {
                "a": "Name",
                "label": "Oklahoma"
            }
        },
        "score": 0.5,
        "fallsWithinCountry": {
            "a": "Country",
            "uri": "http://dig.isi.edu/gazetteer/data/geonames/place/Country/US",
            "hasName": {
                "a": "Name",
                "label": "United States"
            }
        },
        "fallsWithinCountyProvince2ndDiv": {
            "a": "CountyProvince2ndDiv",
            "uri": "http://dig.isi.edu/gazetteer/data/geonames/place/CountyProvince2ndDiv/US_OK_021"
        },
        "geo": {
            "lat": 35.91537,
            "lon": -94.96996
        }
    }
],
"price": 750,
"title": "For Sale: Smith &amp; Wesson 686",
"publisher": {
    "a": "Organization",
    "name": "armslist.com",
    "uri": "http://dig.isi.edu/weapons/data/organization/armslist"
},
"uri": "http://dig.isi.edu/weapons/data/page/13AD9516F01012C5F89E8AADAE5D7E1E2BA97FF9/1433463841000/processed",
"seller": {
    "a": "PersonOrOrganization",
    "description": "Private Party"
} //, ...
}

2 个答案:

答案 0 :(得分:11)

而不是for key, value in jdata:,请使用for key, value in jdata.items():,如下所示:

for key, value in data.items():
    pprint("Key:")
    pprint(key)

查看docs的dict:

  

<强>项()

     

返回字典项目((键,值)对的新视图。)

编辑:如果你想获得所有嵌套密钥而不仅仅是顶级密钥,你可以像another answer中那样采用类似的方法:

def get_keys(dl, keys_list):
    if isinstance(dl, dict):
        keys_list += dl.keys()
        map(lambda x: get_keys(x, keys_list), dl.values())
    elif isinstance(dl, list):
        map(lambda x: get_keys(x, keys_list), dl)

keys = []
get_keys(jdata, keys)

print(keys)
# [u'a', u'inLanguage', u'description', u'priceCurrency', u'geonames_address', u'price', u'title', u'availabl', u'uri', u'seller', u'publisher', u'a', u'hasIdentifier', u'hasPreferredName', u'uri', u'fallsWithinState1stDiv', u'score', u'fallsWithinCountry', u'fallsWithinCountyProvince2ndDiv', u'geo', u'a', u'hasType', u'label', u'a', u'label', u'a', u'uri', u'hasName', u'a', u'label', u'a', u'uri', u'hasName', u'a', u'label', u'a', u'uri', u'lat', u'lon', u'a', u'address', u'a', u'name', u'a', u'description', u'a', u'name', usury']

print(list(set(keys)))    # unique list of keys
# [u'inLanguage', u'fallsWithinState1stDiv', u'label', u'hasName', u'title', u'hasPreferredName', u'lon', u'seller', u'score', u'description', u'price', u'address', u'lat', u'fallsWithinCountyProvince2ndDiv', u'geo', u'a', u'publisher', u'hasIdentifier', u'name', u'priceCurrency', u'geonames_address', u'hasType', u'availabl', u'uri', u'fallsWithinCountry']

答案 1 :(得分:1)

您应该在for key, value in jdata

中使用dict.items()dict.iteritems()

所以,它应该是

for key, value in jdata.items():

OR

for key, value in jdata.iteritems():

分别用于python3和python2。

请参阅此问题的答案,了解两者之间的区别:What is the difference between dict.items() and dict.iteritems()?

如果您只需要迭代字典的键,您甚至可以尝试dict.keys()dict.iterkeys()