1

I am trying to select data with python from what I believe is formatted in JSON. I am using this for loop to select the data as so.

for line_item in response['response']:
   print(line_item['name'] + ', ' + line_item['orderId'])

But how would I also select impressionsDelivered so the output is 1234?

(LineItem){
          orderId = 123456
          name = "fooname"
          stats = (Stats){
                    impressionsDelivered = 1234
                    clicksDelivered = 1234
                    }
5
  • is your response a json? Commented Feb 28, 2017 at 19:00
  • I think it may be. I'm using the google API to pull data from DFP. Commented Feb 28, 2017 at 19:01
  • 1
    then print(line_item['stats']['impressionsDelivered']) should work or are you looking for a more generic way to do it? Commented Feb 28, 2017 at 19:04
  • @YOBA That worked great. Thanks! can you tell me why that worked? Commented Feb 28, 2017 at 19:12
  • 1
    Ok I'll post a detailed response Commented Feb 28, 2017 at 19:13

2 Answers 2

3

Let's say you have this json called response (to use your example):

response = {
          "response":    [   # start of a list of elements
                   {         # start of the 1st element
          "orderId" : 123456,
          "name" : "fooname",
          "stats" : {
                    "impressionsDelivered" : 1234,
                    "clicksDelivered" : 1234
                    }},      # end of the 1st element
                    {        # start of the 2nd element
          "orderId" : 1234565,
          "name" : "fooname2",
          "stats" : {
                    "impressionsDelivered" : 12345,
                    "clicksDelivered" : 12345
                    }}      # end of the second element
                          ] # end of the list
             }

A json is a dictionary like format with keys and values its written: {key:value}

and a value can be itself compose of keys and values example: key: {key2:value2} , {key2:value2} being the value of key

How to access them: You access a dictionary by the keys

in the json above, if I want to access all the values of response:

print( response["response"] )

if you want a deep access to the next element, which is orderId, notice the bracket opening '[' which tells you a list starts:

print( response["response"][0] ) # first element of the list
print( response["response"][0]["orderId"] ) # access orderId

You access a dictionary by its key and a list by its index

Tip: Use Ipython and paste this code to practice

Sign up to request clarification or add additional context in comments.

Comments

0

This question has nothing to do with REST. Just grab your raw response (presumably in JSON format, as you indicated), and then paste it into any online json viewer, then you should be able to figure out how to navigate to the item you want.

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.