3

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.

{"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"}
}

EDIT: an example of the output for this example would be:

confs = [694275295, 729545274, 903187021, 729416761]

or

confs = ['694275295', '729545274', '903187021', '729416761']

it really doesn't matter if they're stored as strings or ints

EDIT 2: here's my code snip:

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) > 1:
        confs.append(int(data[1]["content"]))

print confs
2
  • Can you add an example of the output you need according to this example? Commented Dec 21, 2013 at 21:18
  • just added the example Commented Dec 21, 2013 at 21:29

2 Answers 2

5

Assuming j is your JSON object which the above structure has been parsed into:

>>> results = []
>>> for no in j["response"]["result"]["Potentials"]["row"]:
...     data = no["FL"]
...     if isinstance(data, list) and len(data) > 1:
...         results.append(int(data[1]["content"]))
...
>>> results
[694275295, 729545274, 903187021, 729416761]
Sign up to request clarification or add additional context in comments.

6 Comments

looks like I am getting an error: AttributeError: 'str' object has no attribute 'read'. i've added the snippet of my code into my post.
@user3126085: You first need to parse the string that holds the JSON information using json.load() (from a file) or json.loads() (from a string).
@user3126085: You need to use json.loads(), as I wrote above. Not load() because that only works on file objects.
ooooh jesus. my bad. fixed, and works perfect! thank you so much!
one thing i noticed is if there are duplicate values it stores all of them with an "L" at the end. i checked the raw JSON response, and they are fine there. would you know how to fix that? here's an example: [694275295, 729545274, 5915342892L, 5915342892L] ... EDIT... changing the append to a string got rid of the "L"'s
|
-1

Assuming that 'response' holds the json string:

import json

data = json.loads(response)
rows = data['response']['result']['Potentials']['rows']
output = []
for row in rows:
    contents = row['FL']
    if len(contents) > 1:
        output.append(contents[1]['content'])

That should do it.

EDIT:

I finally got some time to test this "one liner". It's fun to use Python's functional features:

import json

#initialize response to your string
data = json.loads(response)
rows = data['response']['result']['Potentials']['row']
output = [x['FL'][1]['content'] for x in rows if isinstance(x['FL'], list) and len(x['FL']) > 1]
print output

['694275295', '729545274', '903187021', '729416761']

1 Comment

That won't work because if there is only one element, it's not enclosed in a list, so len() will also return 2 because the dictionary has two items in it.

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.