1

I have a json list like this:

{
  "usuarios": [
 {
  "id": 1,
  "nome": "vitor",
  "email": "vitor@"
   }
  ]
}

And I try to list this items using Python like this:

#!/usr/bin/python

import requests
import json
import sys

def list_users():
   response = json.loads(requests.get("http://127.0.0.1:3000/usuarios/")._content)    
   for r in response.get("usuarios"):
       print r["id"],r["nome"],r['email']

if __name__ == '__main__':
    list_users()

And when I run, appear this error:

   python rest_cli.py 
   Traceback (most recent call last):
   File "rest_cli.py", line 62, in <module>
   listar_usuarios()
   File "rest_cli.py", line 10, in listar_usuarios
   for r in response.get("usuarios"):
   AttributeError: 'list' object has no attribute 'get'

How can I fix it?

2
  • 1
    print response before looping and post that here Commented Apr 11, 2017 at 17:15
  • response is apparently a list, so it has not get method like you try to use in response.get("usuarios"). Instead, probably for r in response: .... Commented Apr 11, 2017 at 17:16

2 Answers 2

2

URL in the following code is already giving you an array as response.

response = json.loads(requests.get("http://127.0.0.1:3000/usuarios/")._content)

Don't do a get on the response. Just use a simple for loop on it like :

for r in response:
       print r["id"],r["nome"],r['email']
Sign up to request clarification or add additional context in comments.

Comments

0

That is a namespace issue as you are importing everything from requests I believe. Remove the .get and just use requests. requests("http://127.0.0.1:3000/usuarios/")._content

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.