Python Forum

Full Version: Ip in json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a json
It is necessary to display on the screen in format domains with 144 in 3 octet
un1> domain
un2> domain
This code displays a list of domains with addresses
import urllib.request
import urllib.parse
import json

doc = urllib.request.urlopen("https://api.antizapret.info/diff.php?dateStart=2014-03-25&dateCurrent=2014-03-30&type=json")
data = doc.read()
encoding = doc.info().get_content_charset('utf-8')
i = json.loads(data.decode(encoding))
n = i.get("recordsAdded")
o = len (n)

for m in range(o):
    ip  = i.get("recordsAdded")[m].get ("ip")
    dom  = i.get("recordsAdded")[m].get ("domain")
    print (ip, ">", dom)
Any ideas ?
Quote:
i = json.loads(data.decode(encoding))
n = i.get("recordsAdded")
o = len (n)
 
for m in range(o):
    ip  = i.get("recordsAdded")[m].get ("ip")

Why are you doing so much extra work?  Why not make it simple? 
i = json.loads(data.decode(encoding))
records = i["recordsAdded"]

for record in records:
    ip = record["ip"]
That's unrelated to your question, though.  What's your expected output?  What have you tried?  Each record looks like ip is a comma separated list of ip addresses... are you handling all of them, or just the first?
The output should be like this
ip-address -> domain

104.130.144.203 -> grani.ru
104.130.144.53 -> grani.ru
104.130.144.54 -> grani.ru
etc
for ipaddr in ip.split(","):
    print("{0} -> {1}".format(ipaddr, domain))