在python中打印json文件的所有键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33466450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print All the keys of a json file in python
提问by akira
I have a folder where I have around 20000 JSON files. I want to find out all the unique keys of each JSON and I want take an union of all the keys. However, I got stuck in the initial step only. I am able to find the keys of a single JSON file.
我有一个文件夹,里面有大约 20000 个 JSON 文件。我想找出每个 JSON 的所有唯一键,我想对所有键进行联合。但是,我只停留在初始步骤。我能够找到单个 JSON 文件的键。
I have wrote the following code till now:
到目前为止,我已经编写了以下代码:
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)
It is giving me an error as follows:
它给了我如下错误:
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
My JSON is a nested json. Please suggest me how can I get all the keys.
我的 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 & 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"
} //, ...
}
回答by Mike Covington
Instead of for key, value in jdata:, use for key, value in jdata.items():like this:
而不是for key, value in jdata:,for key, value in jdata.items():像这样使用:
for key, value in data.items():
pprint("Key:")
pprint(key)
Take a look at the docsfor dict:
看看dict的文档:
items():
Return a new view of the dictionary's items ((key, value) pairs).
项目():
返回字典项((键,值)对)的新视图。
EDIT:If you want to get all of the nested keys and not just the top level ones, you could take an approach like those suggested in another answerlike so:
编辑:如果您想获取所有嵌套键,而不仅仅是顶级键,您可以采用类似于另一个答案中建议的方法,如下所示:
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']
回答by Vipul
You should use either dict.items()or dict.iteritems()in for key, value in jdata
您应该使用dict.items()或dict.iteritems()在for key, value in jdata
So, it should be either
所以,它应该是
for key, value in jdata.items():
OR
或者
for key, value in jdata.iteritems():
for python3 and python2 respectively.
分别用于 python3 和 python2。
See answers on this question to know the difference between the two: What is the difference between dict.items() and dict.iteritems()?
请参阅此问题的答案以了解两者之间的区别:dict.items() 和 dict.iteritems() 之间有什么区别?
If you only need to iterate over keys of the dictionary, you can even try dict.keys()or dict.iterkeys()
如果您只需要遍历字典的键,您甚至可以尝试dict.keys()或dict.iterkeys()

