Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get individual userId, id, title and posts from jsonplaceholder.typecode.com

I need help with an assignment regarding API calls. Here are the following parameters:

  1. use python to get on Api (https://jsonplaceholder.typicode.com)

    import requests, json
    url = 'https://jsonplaceholder.typicode.com/posts'
    r = requests.get(url)
    data = json.loads(r.text)
    
  2. capture list of dictionaries from one endpoint and reverse sort

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

    for item in reversed(data):
       print(item)
    
  3. Print a post for specific userId

    print(data[0])
    
  4. Print a post from specific userId that only prints out the title, id, or post

    When I try:

    print(data[0].id[1])
    

I get a ‘dict’ object has no attribute ‘id’. Any help is appreciated. I just need help with question 4.

>Solution :

To access dictionary element in python you use dictionary['<key>'] (not dictionary.<key>). Try:

print(data[0]['id'])
print(data[0]['userId'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading