Just do a simple .keys()
>>> dct = {
... "1": "a",
... "3": "b",
... "8": {
... "12": "c",
... "25": "d"
... }
... }
>>>
>>> dct.keys()
['1', '8', '3']
>>> for key in dct.keys(): print key
...
1
8
3
>>>
If you need a sorted list:
keylist = dct.keys() # this is of type `dict_key`, NOT a `list`
keylist.sort()
And if you want them as simple list, do this:
list(dct_instance.keys())
Answer from karthikr on Stack OverflowJust do a simple .keys()
>>> dct = {
... "1": "a",
... "3": "b",
... "8": {
... "12": "c",
... "25": "d"
... }
... }
>>>
>>> dct.keys()
['1', '8', '3']
>>> for key in dct.keys(): print key
...
1
8
3
>>>
If you need a sorted list:
keylist = dct.keys() # this is of type `dict_key`, NOT a `list`
keylist.sort()
And if you want them as simple list, do this:
list(dct_instance.keys())
for key in data.keys():
print key
Videos
I am learning Python, and in particular, working with JSON and sqlite in Python. Ultimately I plan to use Python to load the JSON into a sqlite database.
Here is the question: Is there a way in to list all of the keys from a JSON file (not from a string) using Python? I want a list of all of the keys so I can determine what columns I will need/use in my sqlite table(s), without having to manually read the file and make a list.
BTW, this is something along the lines of using INFORMATION_SCHEMA.COLUMNS in SQL Server, or the FINDALL in Python for XML.
All of this is for personal learning, so I'm not looking to use other technologies, I'm sticking to Python, JSON, and sqlite on purpose.
If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
What error is it giving you?
If you do exactly this:
data = json.loads('{"lat":444, "lon":555}')
Then:
data['lat']
SHOULD NOT give you any error at all.
Function which return only keys which aren't containing dictionary as their value.
jsonData = {
"a": 1,
"b": 2,
"c": [{
"d": 4,
"e": 5,
"f": {
"g": 6
}
}]
}
def get_simple_keys(data):
result = []
for key in data.keys():
if type(data[key]) != dict:
result.append(key)
else:
result += get_simple_keys(data[key])
return result
print get_simple_keys(jsonData['c'][0])
To avoid using recursion change line result += get_simple_keys(data[key])to result += data[key].keys()
Try this,
dct = {
"a":1,
"b":2,
"c":[
{
"d":4,
"e":5,
"f":{
"g":6
}
}
]
}
k=dct["c"][0]
for key in k.keys():
print key
I have a JSON file that I want to automatically check specific values, like myJSON['generic']['lessgeneric']['store'] should be equal to 100,
was thinking of doing something like this:
checks = [ {'name':'field_1','path':'myJSON['generic']['lessgeneric']['store']','value':'100'} ]
but I have no clue how to convert the string "myJSON['generic']['lessgeneric']['store']" into it's value, any help would be appreciated!
edit: how I solved it
path = "generic>lessgeneric>store"
value = 100
def check_value(path,value):
temp_json = my_json
for key in path.split(">"):
temp_json = temp_json[key]
if temp_json == value:
pass