1

everyone

I started programming in python yesterday to create a project. This consists of taking data from an API using the "Requests" library

So far I had no trouble getting familiar with the library, but I can't get results for what I'm specifically looking for.

My idea is just to get the name of the account.

Here the code

import requests


user = 'example'


payload = {'data': 'username'}
r = requests.get('https://api.imvu.com/user/user-'+user, params=payload)
json = r.json()
print(json)

My idea is that, within all the data that can be obtained, only obtain the name of the account. just the name

The code works perfectly, but it throws me all the account data.

For example:

{
  "https://api.imvu.com/user/user-x?data=created": {
    "data": {
      "created": "2020-11-30T17:56:31Z",
      "registered": "x",
      "gender": "f",
      "display_name": "‏‏‎ ‎",
      "age": "None",
      "country": "None",
      "state": "None",
      "avatar_image": "x",
      "avatar_portrait_image": "https://......",
      "is_vip": false,
      "is_ap": true,
      "is_creator": false,
      "is_adult": true,
      "is_ageverified": true,
      "is_staff": false,
      "is_greeter": false,
      "greeter_score": 0,
      "badge_level": 0,
      "username": "=== ONLY THIS I NEED ==="
    }
  }
}

As you can see, I only need one thing from all that data.

Sorry for bothering and I hope I can learn from your answers. Thanks so much for reading

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Dez Rake
  • 13
  • 2
  • 2
    Unless you designed the API that you are trying to reach here, what you have is the response from the API that cannot change. It is on you to process the response of the API to get what you need. – whiplash Nov 20 '22 at 09:06
  • 1
    You then need to parse the response data. Read up on how to access values from a dictionary. – Gino Mempin Nov 20 '22 at 09:07
  • 2
    Does this answer your question? [How to parse data in JSON format](https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format) – Gino Mempin Nov 20 '22 at 09:08

3 Answers3

1

Unless API allows you to specify exactly what data to return (some does) then you got no control about the API behavior nor what data (and how) given endpoint returns. Publicly exposed API is all you can have in hand and sometimes you may get tons of useless data and there's basically nothing you can do about that.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

you might check whether there is an alternative REST method that only provides you with the username. The REST response you cannot modify as it is sent from the server, so you need to parse the response e.g. like here Extract value from json response python? python

user13322060
  • 52
  • 1
  • 6
0

To get specific item from json, you can simply make few changes in your code.

r = requests.get('https://api.imvu.com/user/user-'+user, params=payload)

json = r.json()

username = json["https://api.imvu.com/user/user-x?data=created"]["data"]["username"]

print(username)
halfelf
  • 9,737
  • 13
  • 54
  • 63