2

I am reading the following nested dictionary in json from an API

{
 "result": [{
               "short_description": "I am getting bluescreen error",
               "sys_id": "39b5f8c2376ede007520021a54990e5c",
               "opened_at": "2016-04-04 05:19:53",
               "number":"INC0258523"
             },
             {
               "short_description": "laptop crashed with a blue screen",
               "sys_id": "da0095380f43d200a4f941fce1050eeb",
               "opened_at":"2016-04-25 06:33:52",
               "number":"INC0259067"
             }, 
             {
               "short_description": "Laptop not booting",
               "sys_id": "ecf9c9b00f43d200a4f941fce1050e17",
               "opened_at": "2016-04-25 06:07:16",
               "number": "INC0259061"
             }]
}

I need to filter the data based on two from and two dates. opened_at is the value containing the date info.

My attempt so far is as follows

    url = "http://ip:port/api"

    response = urllib.urlopen(url)
    data = json.loads(response.read())
    print type(data)
    pattern = 'opened_at'
    element = '2016-04-25 06:33:19'
    with open('D:/Output.csv', 'wb') as f:  
        w = csv.DictWriter(f, data['result'][0].keys()) 
        w.writeheader()
        print type(data['result'])
        for key in data['result']:
            for v, val in data['result'].items():
                if v == pattern and val == element:
                    w.writerow(v)

I get the below error on running the code

AttributeError: 'list' object has no attribute 'items'

I know that the type of data['result'] is a list.Any help will be appreciated. Thanks!

2
  • give some required output, would be easier... Commented May 2, 2016 at 14:11
  • output should be a csv file with contents only between two certain dates. Say I give 04-04-2016 , then the csv file should contain records only made on 04-04-2016. Commented May 3, 2016 at 4:09

3 Answers 3

4

data['result'] is a list of dictionaries, you should iterate on it like follows:

for d in data['result']:
    for k, v in d.items():
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but I am not getting the desired output from it. Nothing is getting written to the output file.
are you sure your element variable contains a value present in your json ? It is not the case in your example.
1

you need this

import json
al = """
{
 "result": [{
               "short_description": "I am getting bluescreen error",
               "sys_id": "39b5f8c2376ede007520021a54990e5c",
               "opened_at": "2016-04-04 05:19:53",
               "number":"INC0258523"
             },
             {
               "short_description": "laptop crashed with a blue screen",
               "sys_id": "da0095380f43d200a4f941fce1050eeb",
               "opened_at":"2016-04-25 06:33:52",
               "number":"INC0259067"
             }, 
             {
               "short_description": "Laptop not booting",
               "sys_id": "ecf9c9b00f43d200a4f941fce1050e17",
               "opened_at": "2016-04-25 06:07:16",
               "number": "INC0259061"
             }]
}
"""
myjson = json.loads(al)
for val in myjson['result']:
    print val['opened_at']
    for key, value in val.items():#change it to keys if required

1 Comment

That's exactly what I have posted 5 min ago, except that you renamed OP variables...
0

Actually "Result" is a dictionnary. You can recognize a dictionnary in Python by the "{" and "}" it is circled with. So here it is a dictionnary of a list of dictionnaries !

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.