1

I was wondering if you could help me with a piece of code i'm working on at the moment. I'm new to Python and this is one of the first major scripts i have tried to write.

import json, sys
from pprint import pprint
#Importing workbench json output into the python script.
with open('jsonoutput.json') as data_file:
    data = json.load(data_file)

#Sets the verible for the while loop.
x = int(0)

while x <= 1:
    y = x
    print type(data)
    jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"]
    if setup_1(jdata) == True:
        Default_1 += 1
    else:
        print "exiting"

Error i get when it runs:

Traceback (most recent call last):
  File "main.py", line 47, in <module>
    jdata = data["result"]["items"][y]["tagValues"]["IdDevicesMap"]["value"]
KeyError: 'tagValues'

Just on a side note as well, when i manually put in the list number [y] to 1 the code runs perfectly. So it's like its got an issue with the way I'm inputting the variable [y] into the request.

1
  • Seems like tagValues is not in some of the nested dictionaries you read from the file. Mind to provide the input ? Commented Feb 10, 2017 at 10:28

2 Answers 2

1

I am pretty sure the json you read doesn't have tagValues in every one of them. You might wanna try try: and except:

import json, sys
from pprint import pprint
#Importing workbench json output into the python script.
with open('jsonoutput.json') as data_file:
    data = json.load(data_file)
x = 0
while True:
    try:
        jdata = data["result"]["items"][x]["tagValues"]["IdDevicesMap"]["value"]
        if setup_1(jdata) == True:
            Default_1 += 1
        else:
            print "exiting"
            break
    except KeyError:
        print data["result"]["items"][x]
        pass
    x+=1

To do it in a pythonic way :

import json, sys
from pprint import pprint
#Importing workbench json output into the python script.
with open('jsonoutput.json') as data_file:
    data = json.load(data_file)

for x, d in enumerate(data["result"]["items"]): #in case you need a counter
    try:
        jdata = d["tagValues"]["IdDevicesMap"]["value"]
        if setup_1(jdata) == True:
            Default_1 += 1
        else:
            print "exiting"
            break
    except KeyError:
        pass
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh awesome, that's exactly what was happening, for some reason i automatically assumed that there was a tagValue for each device, which was incorrect. Changed my code to handle that exception and it worked perfectly fine.
1

This error simply means that you don't have a key called "tagValues". This'll work if your json looks like this.

data = {"result":
         {"items":
           [ {"tagValues":
                 {"IdDevicesMap":
                   {"value":
                     {
                       #data
                     }
                   }
                 }
               }
           ]
         }
       }

So, either your data doesn't look like this, or if it does, then the key called "tagValues" is missing.

Another thing is tell if data["results"]["items"] a list or JSON?

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.