9

I received the following JSON Array from the POST response of an HTTP request:

[{
    "username": "username_1",
    "first_name": "",
    "last_name": "",
    "roles": "system_admin system_user",
    "locale": "en",
    "delete_at": 0,
    "update_at": 1511335509393,
    "create_at": 1511335500662,
    "auth_service": "",
    "email": "userid_1@provider_1.com",
    "auth_data": "",
    "position": "",
    "nickname": "",
    "id": "short-string-of-random-characters-1"
}, {
  ...
}
<more such objects>..]

Given that typeof(response) gives me requests.models.Response, how can I parse it in Python?

Adil B
  • 14,635
  • 11
  • 60
  • 78
Arul
  • 349
  • 2
  • 4
  • 10

4 Answers4

23

Take a look at the json module. More specifically the 'Decoding JSON:' section.

import json
import requests

response = requests.get()  # api call

users = json.loads(response.text)
for user in users:
    print(user['id'])
phoenix
  • 7,988
  • 6
  • 39
  • 45
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
5

You can try like below to get the values from json response:

import json

content=[{
    "username": "admin",
    "first_name": "",
    "last_name": "",
    "roles": "system_admin system_user",
    "locale": "en",
    "delete_at": 0,
    "update_at": 1511335509393,
    "create_at": 1511335500662,
    "auth_service": "",
    "email": "adminuser@cognizant.com",
    "auth_data": "",
    "position": "",
    "nickname": "",
    "id": "pbjds5wmsp8cxr993nmc6ozodh"
}, {
    "username": "chatops",
    "first_name": "",
    "last_name": "",
    "roles": "system_user",
    "locale": "en",
    "delete_at": 0,
    "update_at": 1511335743479,
    "create_at": 1511335743393,
    "auth_service": "",
    "email": "chatops@cognizant.com",
    "auth_data": "",
    "position": "",
    "nickname": "",
    "id": "akxdddp5p7fjirxq7whhntq1nr"
}]

for item in content:
    print("Name: {}\nEmail: {}\nID: {}\n".format(item['username'],item['email'],item['id']))

Output:

Name: admin
Email: adminuser@cognizant.com
ID: pbjds5wmsp8cxr993nmc6ozodh

Name: chatops
Email: chatops@cognizant.com
ID: akxdddp5p7fjirxq7whhntq1nr
MITHU
  • 113
  • 3
  • 12
  • 41
  • 1
    please consider **masking personal information** such as *email* from *snippets* in your answer. See [edit history](https://stackoverflow.com/posts/48189684/revisions) of question for more details – y2k-shubham Oct 05 '18 at 08:35
0

use python 3 and import urlib

import urllib.request
import json
url = link of the server 
#Taking response and request  from url

r = urllib.request.urlopen(url)
#reading and decoding the data
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))


for json_inner_array in data:
        for json_data in json_inner_array:
                    print("id: "+json_data["id"])
Abhishek Kaushik
  • 93
  • 1
  • 2
  • 12
-2

It seems what you are looking for is the json module. with it you can use this to parse a string into json format:

import json
output=json.loads(myJsonString)
Buzz
  • 1,877
  • 21
  • 25