I'm trying to transform a json file to pandas df. This json file has duplicates keys.
Following the answer of this question: Python json parser allow duplicate keys, I tried to do:
from collections import OrderedDict
from json import JSONDecoder
def make_unique(key, dct):
counter = 0
unique_key = key
while unique_key in dct:
counter += 1
unique_key = '{}_{}'.format(key, counter)
return unique_key
def parse_object_pairs(pairs):
dct = OrderedDict()
for key, value in pairs:
if key in dct:
key = make_unique(key, dct)
dct[key] = value
return dct
decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
with open("file.json") as f:
obj = decoder.decode(f)
#print obj
I received the following error:
TypeError
Traceback (most recent call last)
<ipython-input-70-0d2633348c10> in <module>()
2
3 with open("file.json") as f:
----> 4 obj = decoder.decode(f)
5 #print obj
6
C:\ProgramData\Anaconda2\lib\json\decoder.pyc in decode(self, s, _w)
362
363 """
--> 364 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
365 end = _w(s, end).end()
366 if end != len(s):
TypeError: expected string or buffer"
What am I missing?