Skip to content

Instantly share code, notes, and snippets.

@koddr
Created April 2, 2019 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koddr/1335333ef927a8ac1cd6274aea7d25c3 to your computer and use it in GitHub Desktop.
Save koddr/1335333ef927a8ac1cd6274aea7d25c3 to your computer and use it in GitHub Desktop.
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