Skip to main content

Python : reading json data in python


The below program shows how to read a json file and pull the data element for it in python. We make use json library for this.
#!/usr/bin/python

import json

def get_id():
  file = open("/home/python/json/js.json","r")
  json_file = file.read()
  parse_json = json.loads(json_file)
  a = parse_json['purchase'][0]['order_id']
  print a

get_id()

#output : 1234

Sample json file

{
   "store" : "New York",
 
   "purchase" :
   [
   {
      "order_id" : "1234",
      "customer" : "regular",  
      "amount" : "$123.45"
   }
   ]
}

Comments