RoyMWell
RoyMWell

Reputation: 199

loop through json elements for a list of values

I have the following code:

#!/usr/bin/python2.7
import json, re, sys
x = json.loads('''{"status":{"code":"200","msg":"ok","stackTrace":null},"dbTimeCost":11,"totalTimeCost":12,"hasmore":false,"count":5,"result":[{"_type":"Compute","_oid":"555e262fe4b059c7fbd6af72","label":"lvs3b01c-ea7c.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e27d8e4b059c7fbd6bab9","label":"lvs3b01c-9073.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e27c9e4b059c7fbd6ba7e","label":"lvs3b01c-b14b.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e2798e4b0800601a83b0f","label":"lvs3b01c-6ae2.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e2693e4b087582f108200","label":"lvs3b01c-a228.stratus.lvs.ebay.com"}]}''')
print x['result'][4]['label']
sys.exit()

The desired result should be all the labels. But, when I run it, it only prints the first label. What am I doing wrong here?

And also, before I could figure out that "result" was the key to use, I needed to copy and paste the json data to a site like "jsonlint.com" to reformat it in a readable fashion. Im wondering if there's a better way to do that, preferably without having to copy and paste the json data anywhere.

So two questions:

  1. How do I get the above code to list all the labels
  2. How do I know the key to the field I want, without having to reformat the given ugly one liner json data

Upvotes: 0

Views: 130

Answers (3)

backtrack
backtrack

Reputation: 8144

You need to loop all the result. You result is a list of object which has the label.

labels = [ i.get('label') for i in x.get('result')]
print(labels)

Use .get() it will not return None if the key is not available.

Upvotes: 1

abhi krishnan
abhi krishnan

Reputation: 836

just change your code which is used to print the label

print x['result'][4]['label'] # here you are just printing the 4th label only

to

 print [i["label"] for i in x['result']]

Upvotes: 2

Rakesh
Rakesh

Reputation: 82755

Using a list comprehension to get all label

Ex:

import json, re, sys
x = json.loads('''{"status":{"code":"200","msg":"ok","stackTrace":null},"dbTimeCost":11,"totalTimeCost":12,"hasmore":false,"count":5,"result":[{"_type":"Compute","_oid":"555e262fe4b059c7fbd6af72","label":"lvs3b01c-ea7c.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e27d8e4b059c7fbd6bab9","label":"lvs3b01c-9073.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e27c9e4b059c7fbd6ba7e","label":"lvs3b01c-b14b.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e2798e4b0800601a83b0f","label":"lvs3b01c-6ae2.stratus.lvs.ebay.com"},{"_type":"Compute","_oid":"555e2693e4b087582f108200","label":"lvs3b01c-a228.stratus.lvs.ebay.com"}]}''')
print [i["label"] for i in x['result']]
sys.exit()

Output:

[u'lvs3b01c-ea7c.stratus.lvs.ebay.com', u'lvs3b01c-9073.stratus.lvs.ebay.com', u'lvs3b01c-b14b.stratus.lvs.ebay.com', u'lvs3b01c-6ae2.stratus.lvs.ebay.com', u'lvs3b01c-a228.stratus.lvs.ebay.com']

You can use pprint to view your JSON in a better format.

Ex:

import pprint
pprint.pprint(x)

Output:

{u'count': 5,
 u'dbTimeCost': 11,
 u'hasmore': False,
 u'result': [{u'_oid': u'555e262fe4b059c7fbd6af72',
              u'_type': u'Compute',
              u'label': u'lvs3b01c-ea7c.stratus.lvs.ebay.com'},
             {u'_oid': u'555e27d8e4b059c7fbd6bab9',
              u'_type': u'Compute',
              u'label': u'lvs3b01c-9073.stratus.lvs.ebay.com'},
             {u'_oid': u'555e27c9e4b059c7fbd6ba7e',
              u'_type': u'Compute',
              u'label': u'lvs3b01c-b14b.stratus.lvs.ebay.com'},
             {u'_oid': u'555e2798e4b0800601a83b0f',
              u'_type': u'Compute',
              u'label': u'lvs3b01c-6ae2.stratus.lvs.ebay.com'},
             {u'_oid': u'555e2693e4b087582f108200',
              u'_type': u'Compute',
              u'label': u'lvs3b01c-a228.stratus.lvs.ebay.com'}],
 u'status': {u'code': u'200', u'msg': u'ok', u'stackTrace': None},
 u'totalTimeCost': 12}

Upvotes: 0

Related Questions