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

Question:

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

    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.

Asked By: user2816227

||

Answers:

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

print(data[0]['id'])
print(data[0]['userId'])
Answered By: Yevhen Kuzmovych
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.