2

I'm trying to parse some Json, my code goes like this:

Json = '{"status":"Success", "resultsMethod":"database", "lastScrape":"2012-05-28 00:03:52", "domainCount":"45", "remoteAddress":"www.digg.com", "remoteIpAddress":"64.191.203.30", "domainArray":[["567gu.com", ""], ["64.191.203.30", ""], ["64.191.203.30.", ""], ["bellacor.com", ""], ["blogs.vancouversun.com", ""], ["da-dk.chinathin.com", ""], ["dads.new.digg.com", ""], ["deathofaclown.com", ""], ["digg-inc.com", ""], ["digg.bokefu.com", ""], ["digg.com", ""], ["digg.com.", ""], ["diggcontest.com", ""], ["diggcorp.com", ""], ["diggentertainment.com", ""], ["diggriver.com", ""], ["e.digg.com", ""], ["evercleancanada.com", ""], ["feeds.digg.com", ""], ["feeds.gawker.com", ""], ["feeds.paidcontent.org", ""], ["firthfamily.net", ""], ["m.digg.com", ""], ["media.digg.com", ""], ["new.digg.com", ""], ["rfcoron.no-ip.org", ""], ["rss.digg.com", ""], ["samtesta.com", ""], ["services.digg.com", ""], ["shopzilla.com", ""], ["theultimatebathrooms.com", ""], ["tmpnetwork.com", ""], ["www.567gu.com", ""], ["www.businesswire.com", ""], ["www.calibex.com", ""], ["www.digg-inc.com", ""], ["www.digg.com", ""], ["www.freshnews.org", ""], ["www.iconfactory.com", ""], ["www.istoleyoursite.com", ""], ["www.lethargy.org", ""], ["www.pheedcontent.com", ""], ["www.stuff.co.nz", ""], ["www.tr.im", ""], ["www.xkan.cn", ""]]}'


obj = json.loads(Json)
for key, value in obj.iteritems():
    print key,value

This above example print this :

status Success
domainArray [[u'567gu.com', u''], [u'64.191.203.30', u''], [u'64.191.203.30.', u''], [u'bellacor.com', u''], [u'blogs.vancouversun.com', u''], [u'da-dk.chinathin.com', u''], [u'dads.new.digg.com', u''], [u'deathofaclown.com', u''], [u'digg-inc.com', u''], [u'digg.bokefu.com', u''], [u'digg.com', u''], [u'digg.com.', u''], [u'diggcontest.com', u''], [u'diggcorp.com', u''], [u'diggentertainment.com', u''], [u'diggriver.com', u''], [u'e.digg.com', u''], [u'evercleancanada.com', u''], [u'feeds.digg.com', u''], [u'feeds.gawker.com', u''], [u'feeds.paidcontent.org', u''], [u'firthfamily.net', u''], [u'm.digg.com', u''], [u'media.digg.com', u''], [u'new.digg.com', u''], [u'rfcoron.no-ip.org', u''], [u'rss.digg.com', u''], [u'samtesta.com', u''], [u'services.digg.com', u''], [u'shopzilla.com', u''], [u'theultimatebathrooms.com', u''], [u'tmpnetwork.com', u''], [u'www.567gu.com', u''], [u'www.businesswire.com', u''], [u'www.calibex.com', u''], [u'www.digg-inc.com', u''], [u'www.digg.com', u''], [u'www.freshnews.org', u''], [u'www.iconfactory.com', u''], [u'www.istoleyoursite.com', u''], [u'www.lethargy.org', u''], [u'www.pheedcontent.com', u''], [u'www.stuff.co.nz', u''], [u'www.tr.im', u''], [u'www.xkan.cn', u'']]
lastScrape 2012-05-28 00:03:52
remoteAddress www.digg.com
resultsMethod database
remoteIpAddress 64.191.203.30
domainCount 45

I really would like to have domainCount, RemoteIpAddress and RemoteAddress shown first and last but not least; Have a normal string list for the domainArray key. In this example, domainArray key correspond to a list that is shown like this: [[u'567gu.com', u''],

I would like to have a list like this:

DomainArray : 
ellacor.com
blogs.vancouversun.com
da-dk.chinathin.com
etc, etc.

Any help would be greatly appreciated !

Thanks

2 Answers 2

5
obj = json.loads(Json)
print("DomainCount: %d" % obj['domainCount'])
print("DomainArray: %s" % '\n'.join(e[0] for e in obj['domainArray'])
# etc
Sign up to request clarification or add additional context in comments.

1 Comment

@user1473508: Dictionaries are unordered, so just by returning the key-value pairs, you can't guarantee the order they are in.
1
try:
    import simplejson as json
except ImportError:
    import json

s = '{"status":"Success", "resultsMethod":"database", "lastScrape":"2012-05-28 00:03:52", "domainCount":"45", "remoteAddress":"www.digg.com", "remoteIpAddress":"64.191.203.30", "domainArray":[["567gu.com", ""], ["64.191.203.30", ""], ["64.191.203.30.", ""], ["bellacor.com", ""], ["blogs.vancouversun.com", ""], ["da-dk.chinathin.com", ""], ["dads.new.digg.com", ""], ["deathofaclown.com", ""], ["digg-inc.com", ""], ["digg.bokefu.com", ""], ["digg.com", ""], ["digg.com.", ""], ["diggcontest.com", ""], ["diggcorp.com", ""], ["diggentertainment.com", ""], ["diggriver.com", ""], ["e.digg.com", ""], ["evercleancanada.com", ""], ["feeds.digg.com", ""], ["feeds.gawker.com", ""], ["feeds.paidcontent.org", ""], ["firthfamily.net", ""], ["m.digg.com", ""], ["media.digg.com", ""], ["new.digg.com", ""], ["rfcoron.no-ip.org", ""], ["rss.digg.com", ""], ["samtesta.com", ""], ["services.digg.com", ""], ["shopzilla.com", ""], ["theultimatebathrooms.com", ""], ["tmpnetwork.com", ""], ["www.567gu.com", ""], ["www.businesswire.com", ""], ["www.calibex.com", ""], ["www.digg-inc.com", ""], ["www.digg.com", ""], ["www.freshnews.org", ""], ["www.iconfactory.com", ""], ["www.istoleyoursite.com", ""], ["www.lethargy.org", ""], ["www.pheedcontent.com", ""], ["www.stuff.co.nz", ""], ["www.tr.im", ""], ["www.xkan.cn", ""]]}'
obj = json.loads(s)

printorder = ['domainCount', 'remoteIpAddress', 'remoteAddress', 'lastScrape', 'resultsMethod', 'status', 'domainArray']
for var,val in ((var, obj.get(var)) for var in printorder):
    if var == 'domainArray':
        val = ''.join('\n  {}'.format(domain[0]) for domain in val)
    var = '{}:'.format(var)
    print('{:<20} {}'.format(var, val))

results in

domainCount:         45
remoteIpAddress:     64.191.203.30
remoteAddress:       www.digg.com
lastScrape:          2012-05-28 00:03:52
resultsMethod:       database
status:              Success
domainArray:         
  567gu.com
  64.191.203.30
  64.191.203.30.
  bellacor.com
  blogs.vancouversun.com
  da-dk.chinathin.com
  dads.new.digg.com
  deathofaclown.com
  digg-inc.com
  digg.bokefu.com
  digg.com
  digg.com.
  diggcontest.com
  diggcorp.com
  diggentertainment.com
  diggriver.com
  e.digg.com
  evercleancanada.com
  feeds.digg.com
  feeds.gawker.com
  feeds.paidcontent.org
  firthfamily.net
  m.digg.com
  media.digg.com
  new.digg.com
  rfcoron.no-ip.org
  rss.digg.com
  samtesta.com
  services.digg.com
  shopzilla.com
  theultimatebathrooms.com
  tmpnetwork.com
  www.567gu.com
  www.businesswire.com
  www.calibex.com
  www.digg-inc.com
  www.digg.com
  www.freshnews.org
  www.iconfactory.com
  www.istoleyoursite.com
  www.lethargy.org
  www.pheedcontent.com
  www.stuff.co.nz
  www.tr.im
  www.xkan.cn            

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.