10

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?

4
  • You need to use the json module. Commented Jan 10, 2018 at 14:36
  • Is it an array or a string? Commented Jan 10, 2018 at 14:39
  • Possible duplicate of Convert string to JSON using Python if not, then it's an array which means its already parsed as json, so I'm not sure what you're asking in that case Commented Jan 10, 2018 at 14:39
  • 1
    you might want to mask personal information such as email, phone etc from your snippets before posting on public forums Commented Oct 5, 2018 at 8:23

4 Answers 4

25

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

Its not required for you to import json, Its simple python dict and use for loop without loading to json.
@prashantthakre How can you loop over the dict without first json decoding the string?
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": "[email protected]",
    "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": "[email protected]",
    "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: [email protected]
ID: pbjds5wmsp8cxr993nmc6ozodh

Name: chatops
Email: [email protected]
ID: akxdddp5p7fjirxq7whhntq1nr

1 Comment

please consider masking personal information such as email from snippets in your answer. See edit history of question for more details
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"])

Comments

-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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.