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"]
?
You could pass
object_pairs_hook
tojson.loads
that will collect the values with same keys to lists:Output:
In above hook receives
list
of(key, value)
tuples that it stores todefaultdict
where values are lists. Once it has iterated over the tuples it will generate resultdict
where value is list if there were multiple items with given key.Python documentation has following description of the hook: