Note that there are some explanatory texts on larger screens.

plurals
  1. POpython + json: parse to list
    primarykey
    data
    text
    <p>I'm somewhat new to parsing JSON data with python (using python 2.7). There is a service that I have to send API calls to, and the JSON response is something like what I have below. the amount of items in 'row' can vary. What I need to do is take only the 'content' from the second line IF there is a second line, and put it into a list. Essentially, it is a list of only the 'campaign confirmation numbers' and nothing else. the number will also always be only 9 numeric numbers if that helps anything. Any advice would be very much appreciated.</p> <pre><code>{"response": {"result": {"Potentials": {"row": [ {"no":"1","FL": {"content":"523836000004148171","val":"POTENTIALID"} }, {"no":"2","FL": {"content":"523836000004924051","val":"POTENTIALID"} }, {"no":"3","FL": [ {"content":"523836000005318448","val":"POTENTIALID"}, {"content":"694275295","val":"Campaign Confirmation Number"} ] }, {"no":"4","FL": [ {"content":"523836000005318662","val":"POTENTIALID"}, {"content":"729545274","val":"Campaign Confirmation Number"} ] }, {"no":"5","FL": [ {"content":"523836000005318663","val":"POTENTIALID"}, {"content":"903187021","val":"Campaign Confirmation Number"} ] }, {"no":"6","FL": {"content":"523836000005322387","val":"POTENTIALID"} }, {"no":"7","FL": [ {"content":"523836000005332558","val":"POTENTIALID"}, {"content":"729416761","val":"Campaign Confirmation Number"} ] } ] } }, "uri":"/crm/private/json/Potentials/getSearchRecords"} } </code></pre> <p>EDIT: an example of the output for this example would be:</p> <p>confs = [694275295, 729545274, 903187021, 729416761]</p> <p>or</p> <p>confs = ['694275295', '729545274', '903187021', '729416761']</p> <p>it really doesn't matter if they're stored as strings or ints</p> <p>EDIT 2: here's my code snip:</p> <pre><code>import urllib import urllib2 import datetime import json key = '[removed]' params = { '[removed]' } final_URL = 'https://[removed]' data = urllib.urlencode(params) request = urllib2.Request(final_URL,data) response = urllib2.urlopen(request) content = response.read() j = json.load(content) confs = [] for no in j["response"]["result"]["Potentials"]["row"]: data = no["FL"] if isinstance(data, list) and len(data) &gt; 1: confs.append(int(data[1]["content"])) print confs </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload