Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python: JSON only get keys in first level
import json
string_arr = '''
{
"user": {
"code": 0,
"data": [
{
"1": {
"row": {
"id": 1,
"name": "User 1"
}
}
},
{
"2": {
"row": {
"id": 2,
"name": "User 2"
}
}
}
],
"message": "Done",
"http_status_code": 200
}
}
'''
arr = json.loads(string_arr)
keylist = arr['user']['data']
id_list = []
result = []
for id in keylist:
id_list.append(list(id.keys())[0])
print('')
print('ID array:')
print('')
print(id_list)
for key in keylist:
result.append(list(key.values())[0])
print('')
print('Result array:')
print('')
print(result)
print('')
print('Result array > row name:')
print('')
for r in result:
print(r['row']['name'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.