1

I'm trying to parse the following json using python -

{
   "sentAt":"2016-06-11T04:48:25Z",
   "Device":"iPhone6",
   "batch":[
      {
         "URL":"/laundry/Vanish",
         "event":"PageView",
         "createdAt":"2016-06-11T04:47:58Z"
      },
      {
         "listPrice":6.5,
         "product_id":47446,
         "category":"",
         "salePrice":5.2,
         "eventName":"ViewProduct",
         "prodName":"Vanish",
         "createdAt":"2016-06-11T04:47:59Z"
      }
   ]
}

I'm able to parse the above json using the below python code -

import json
from pprint import pprint

with open('file.json') as data_file:    
    data = json.load(data_file)

print "Value1 - " , data["sentAt"].encode('utf-8')
print "Value2  - ", data["batch"][0]["URL"].encode('utf-8')
print "Value3 - ", data["batch"][0]["event"].encode('utf-8')

I'm able to print value1 through value3. But I want to know if there is a way to access json of the following type

{
   "sentAt":"2016-06-11T04:48:25Z",
   "Device":"iPhone6",
   "batch":[
      {
         "URL":"/laundry/Vanish",
         "event":"PageView",
         "createdAt":"2016-06-11T04:47:58Z"
      },
      {
         "listPrice":6.5,
         "product_id":47446,
         "category":"",
         "salePrice":5.2,
         "eventName":"ViewProduct",
         "prodName":"Vanish",
         "createdAt":"2016-06-11T04:47:59Z"
      }
   ]
}
{
   "sentAt":"2016-07-11T04:48:25Z",
   "Device":"iPhone5",
   "batch":[
      {
         "URL":"/laundry/NewProduct",
         "event":"NewPage",
         "createdAt":"2016-06-11T04:47:58Z"
      },
      {
         "listPrice":6.5,
         "product_id":47446,
         "category":"",
         "salePrice":5.2,
         "eventName":"ViewProduct",
         "prodName":"Vanish",
         "createdAt":"2016-06-11T04:47:59Z"
      },
   ]
}

As you can see the above JSON has 2 dictionaries and I want to access the data inside the 2nd json dictionary.

For example - I want to print URL - /laundry/NewProduct and not URL - /laundry/Vanish

FirstName
  • 377
  • 2
  • 6
  • 21

0 Answers0