Python- How do I retrieve the multiple values associated with the same key in a dictionary-like json string?

1k Views Asked by At

OK, so I have json source code from a webpage and, in this source code, the same word("author") is used as the key for multiple values. How can I retrieve all the values for "author"?

E.g.

"author": "SampleMan", "author":"NonSampleMan", "author":"BoringMan"

How do I get Python to return ["SampleMan", "NonSampleMan", "BoringMan"]?

1

There are 1 best solutions below

0
On

You could pass object_pairs_hook to json.loads that will collect the values with same keys to lists:

from collections import defaultdict
import json

s = '{"author": "SampleMan", "author":"NonSampleMan", "author":"BoringMan", "foo":"bar", "bar": [1]}'

def hook(pairs):
    d = defaultdict(list)
    for k, v in pairs:
        d[k].append(v)

    return {k: v if len(v) > 1 else v[0] for k, v in d.items()}

print(json.loads(s, object_pairs_hook=hook))

Output:

{'bar': [1], 'author': ['SampleMan', 'NonSampleMan', 'BoringMan'], 'foo': 'bar'}

In above hook receives list of (key, value) tuples that it stores to defaultdict where values are lists. Once it has iterated over the tuples it will generate result dict where value is list if there were multiple items with given key.

Python documentation has following description of the hook:

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.