0

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
0

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have already figured that out. I am just trying to parse each individual JSONstring so I can write/print it to the console.
0

try json.dumps, like this

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

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.