-4

Please help me to parse this json in python.

{ "IT" : [   
                            { "firstName" : "ajay",  
                              "lastName"  : "stha",
                              "age"       : 24 },

                            { "firstName" : "Michiel",  
                              "lastName"  : "Og",
                              "age"       : 35 }
                          ],                            
          "sales"       : [ 
                            { "firstName" : "Guru", 
                              "lastName"  : "red",
                              "age"       : 27 },

                            { "firstName" : "Jim",   
                              "lastName"  : "Galley",
                              "age"       : 34 }
                          ] 
        } 

How to parse this json in Python?Please help me

backcross
  • 93
  • 1
  • 9

3 Answers3

5

Using json:

import json
data = json.loads(stringinput)
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
2
import json
jsonResponse = json.loads(data)
jsonDataSales = jsonResponse["sales"]
jsonDataIt = jsonResponse["IT"]
it_first_name_list = []
it_last_name_list = []
it_age_list = []
sales_first_name_list = []
sales_last_name_list = []
sales_age_list = []

for item in jsonDataIt:
    it_first_name_list.append(item.get("firstName"))
    it_last_name_list.append(item.get("lastName"))
    it_age_list.append(item.get("age"))

for item in jsonDataSales:
    sales_first_name_list.append(item.get("firstName"))
    sales_last_name_list.append(item.get("lastName"))
    sales_age_list.append(item.get("age"))
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
1

Python dictionary with nested structures is very similar to JSON data, though Python’s variables and expressions support richer structuring options (any part of the following can be an arbitrary expression in Python code) for eg.

>>> name = dict(first='Bob', last='Smith')
>>> rec = dict(name=name, job=['dev', 'mgr'], age=40.5)
>>> rec
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}

The final dictionary format displayed here is a valid literal in Python code, and almost passes for JSON when printed as is, but the json module makes the translation official —here translating Python objects to and from a JSON serialized string representation in memory:

>>> import json
>>> json.dumps(rec)
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> S = json.dumps(rec)
>>> S
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> O = json.loads(S)
>>> O
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}
>>> O == rec
True

It’s similarly straightforward to translate Python objects to and from JSON data strings in files. Prior to being stored in a file, your data is simply Python objects; the JSON module recreates them from the JSON textual representation when it loads it from the file:

>>> json.dump(rec, fp=open('testjson.txt', 'w'), indent=4)
>>> print(open('testjson.txt').read())
{
"job": [
"dev",
"mgr"
],
"name": {
"last": "Smith",
"first": "Bob"
},
"age": 40.5
}
>>> P = json.load(open('testjson.txt'))
>>> P
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}
Pavan Gupta
  • 17,663
  • 4
  • 22
  • 29