Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json in Python: Receive/Check duplicate key error

Tags:

python

json

The json module of python acts a little of the specification when having duplicate keys in a map:

import json
>>> json.loads('{"a": "First", "a": "Second"}')
{u'a': u'Second'}

I know that this behaviour is specified in the documentation:

The RFC specifies that the names within a JSON object should be unique, but does not specify how repeated names in JSON objects should be handled. By default, this module does not raise an exception; instead, it ignores all but the last name-value pair for a given name:

For my current project, I absolutely need to make sure that no duplicate keys are present in the file and receive an error/exception if this is the case? How can this be accomplished?

I'm still stuck on Python 2.7, so a solution which also works with older versions would help me most.

like image 995
theomega Avatar asked Apr 23 '13 14:04

theomega


People also ask

Can JSON have duplicate keys Python?

python - json. loads allows duplicate keys in a dictionary, overwriting the first value - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

How does Python handle JSON key errors?

To solve the (JSON) KeyError exception in Python, use the json. loads() method to parse the JSON string into a native Python object and conditionally check if the key is present in the dictionary before accessing it.

Can JSON array have duplicate keys?

The short answer: Yes but is not recommended.

Can JSON have same key?

There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used. In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name".


1 Answers

Well, you could try using the JSONDecoder class and specifying a custom object_pairs_hook, which will receive the duplicates before they would get deduped.

import json

def dupe_checking_hook(pairs):
    result = dict()
    for key,val in pairs:
        if key in result:
            raise KeyError("Duplicate key specified: %s" % key)
        result[key] = val
    return result

decoder = json.JSONDecoder(object_pairs_hook=dupe_checking_hook)

# Raises a KeyError
some_json = decoder.decode('''{"a":"hi","a":"bye"}''')

# works
some_json = decoder.decode('''{"a":"hi","b":"bye"}''')
like image 102
Amber Avatar answered Sep 23 '22 12:09

Amber