Search code examples
pythonjsondictionaryrobotframeworkkeyerror

Key error u'True when checking for a value in JSON dump stored in python dictionary


I have piece of code that retrieves the a JSON dump from a location and check for values in the stored dictionary.

My JSON dump looks like this:

{
    u'response': {
        u'username': u'robo',
        u'first_name': u'robot-update',
        u'last_name': u'frmwrk-update',
        u'is_deleted': False,
        u'entity_type': u'user',
        u'admin': True,
        u'image': {
            u'complete_json': True,
            u'entity_type': u'image',
            u'original': u'/images/default-user-icon.png',
            u'icon': u'/images/default-user-icon.png'
        },
        u'title': u'update-title',
        u'email': u'robotupdate@robot.com',
        u'dept': u'update-department',
        u'subscribed_to_emails': True,
        u'notes': u'update-note',
        u'complete_json': True,
        u'id': 177,
        u'tags': [],
        u'developer': True
    }
}

I am trying to retrieve a key value pair and compare it with my expected value.

My code snippet is as follows user_info gets back my json dumps:

    def check_user_info(self, user_name, key_value, expected_value):
        logger.info("Checking to see if the - {0} is the same as the expected value       {1}...".format(field_value, expected_value))
        user_info = self.my_api.get_user_info(self.token, self.app_address, user_name)
        logger.console("json dumps------ {0}".format(user_info))
        logger.console("the check value is-- {0}".format(user_info['response'][field_value]))
        check_value = user_info['response'][field_value]
        logger.info("field value is {0}".format(user_info['response'][field_value]))
        if check_value == expected_value:
            logger.info("The test value {0} is the same as expected value  {1}".format(check_value, expected_value))

When I try to look up the values for first name, last name or email. I get through the method. But when I try to get the admin value I get a keyerror u'True

Please kindly let me know what I am doing wrong


Solution

  • The first rule of debugging is to assume that the error message is telling you the truth. In this case it is telling you keyerror u'True.

    This means that somewhere in your code you're doing the equivalent of some_dictionary[u'True']. In the code you posted, there is nothing that is using the literal value True as a key, so it must be in a variable. Since you didn't post the actual error, I have to guess which line is causing the error.

    Most likely, the error is coming from this code: user_info['response'][field_value]). That means that field_value is likely u'True'. The question now becomes, why is it true? Since your code doesn't show where that value comes from, there's no way I can answer that. You'll have to do some additional debugging where you set that variable.

    Looking at the name of the variable, it appears you're expecting the variable to contain a value, but you're using it as a key. Perhaps that is the problem? Maybe that line of code should be user_info['response'][user_name] (ie: replace key_value with user_name)?