Created
April 2, 2019 08:35
-
-
Save koddr/1335333ef927a8ac1cd6274aea7d25c3 to your computer and use it in GitHub Desktop.
Python: JSON only get keys in first level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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