Arilis
Arilis

Reputation: 11

Trying to parse JSON data from a url with python

I am trying to make a IP details grabber via python and a JSON API but I am having trouble parsing the JSON data.

I've tried loading and dumping the data, none of that works so I have 0 idea how I am going to parse this data.

#Importing
import requests
import json
import os

#Variables
cls = os.system('cls')
#Startup

cls #Clearing the console on startup

ipToSearch = input("Please enter the IP you wish to search: ")
saveDetails = input("Would you like to save the IP's deatils to a file? Y/N: ")

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.loads(ip_JSON)
print(ip_Data)

I am trying to parse the IP's information but the result is this error currently.

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    ip_Data = json.loads(ip_JSON)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict

Upvotes: 0

Views: 4173

Answers (2)

Nguyen Tan Bao
Nguyen Tan Bao

Reputation: 111

try json.dumps, like this

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.dumps(ip_JSON)

Upvotes: 0

Fredrik
Fredrik

Reputation: 451

The traceback is because it looks like you already converted it to json on the previous line .json() and then you try to do it again.

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.loads(ip_JSON)

Try

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
print(ip_JSON)

Upvotes: 1

Related Questions