0

I'm trying to parse the JSON file which has the ID as key and response headers as values.Im trying to fetch the IDs and its particular value(server details). #1.JSON

{
    "1": {
        "content-length": "51828",
        "expires": "Fri, 19 Feb 2016 02:38:36 GMT",
        "x-powered-by": "W3 Total Cache/0.9.4.1",
        "accept-ranges": "bytes",
        "strict-transport-security": "max-age=31536000",
        "vary": "Accept-Encoding,Cookie",
        "server": "Apache/2.2.22 (Ubuntu)",
        "last-modified": "Fri, 12 Feb 2016 02:38:36 GMT",
        "etag": "\"ca74-52b89905bc300\"",
        "pragma": "public",
        "cache-control": "max-age=600, private, must-revalidate, public",
        "date": "Mon, 15 Feb 2016 10:06:58 GMT",
        "x-frame-options": "SAMEORIGIN",
        "content-type": "text/html; charset=UTF-8"
    },
    "2": {
        "content-length": "0",
        "content-language": "en",
        "x-powered-by": "Servlet/2.5 JSP/2.1",
        "set-cookie": "JSESSIONID=hdkyWBjGT5YD0GSG9wMRSGVybnlLvhvwKHMc0PJdxN8JbKclygWQ!919258543!229342577; path=/; HttpOnly",
        "x-oracle-dms-ecid": "549485340858380",
        "server": "BigIP",
        "connection": "close",
        "location": "http://www.oracle.com/us/corporate/acquisitions/bigmachines/index.html",
        "date": "Mon, 15 Feb 2016 10:07:02 GMT",
        "access-control-allow-origin": "*",
        "content-type": "text/html; charset=utf-8",
        "x-frame-options": "SAMEORIGIN"
    }
}

I tried,

#Check.py
import json

with open('json_data.json') as json_data:
    data = json.load(json_data)
    print data

for key,value in data.iteritems():
    if 'server' in value:
        for appid,headers in value['server'].iteritems():
            print appid,","headers

I get the following error,

for appid,headers in value['server'].iteritems(): AttributeError: 'unicode' object has no attribute 'iteritems'

Expected Result :  

1,Apache/2.2.22 (Ubuntu)
2,BigIP

Not sure why i'm getting this issue ? Any suggestions please ?

1
  • value['server'] is a string, e.g. "Apache/2.2.22 (Ubuntu)". You cannot iterate over that. Commented Feb 15, 2016 at 10:54

1 Answer 1

2

second part should be:

for key,value in data.iteritems() :
    if 'server' in value:
        print key+","+value['server']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.