-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

3
  • 5
    docs.python.org/2/library/json.html should provide you all the information you need Commented Jun 4, 2014 at 9:18
  • 1
    -1 for failing to do a simple Google search before posting. Commented Jun 4, 2014 at 9:31
  • @DanielRoseman What the problem with you?I got problem and ask.do you have any problem with this? Commented Jun 4, 2014 at 9:34

3 Answers 3

5

Using json:

import json
data = json.loads(stringinput)
Sign up to request clarification or add additional context in comments.

Comments

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"))

Comments

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}

1 Comment

@backcross if you find it useful then please mark this answer as right so that others can get benefited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.