I have a JSON string with duplicate entries. I already learned how to convert those into an array to preserve all of them when calling json.loads(string), see e.g. https://stackoverflow.com/a/61416136/7471760.
The question now, in some cases, I have very weird input JSON strings (which I cannot change) where I really need to preserve the order also among 'different' sets of duplicates. For example:
jstring = '\
{\
"anna": { "age": 23, "color": "green"},\
"john": { "age": 35, "color": "blue"},\
"laura":{ "age": 32, "color": "red"},\
"john": { "age": 31, "color": "black"},\
"anna": { "age": 41, "color": "pink"}\
}'
Now I use this hook to convert this string into a JSON object, but without loosing the duplicates (different students).
def array_on_duplicates(ordered_pairs):
d = {}
for k, v in ordered_pairs:
if k in d:
if type(d[k]) is list:
d[k].append(v)
else:
d[k] = [d[k],v]
else:
d[k] = v
return d
However, I still need to recover the original list order of these students (first input - first ouput).
When using json.loads(), I have all entries, but I lose that original order:
json.loads(jstring, object_pairs_hook=array_on_duplicates)
{'anna': [{'age': 23, 'color': 'green'}, {'age': 41, 'color': 'pink'}],
'john': [{'age': 35, 'color': 'blue'}, {'age': 31, 'color': 'black'}],
'laura': {'age': 32, 'color': 'red'}}
What would be the most efficient way around this problem? (apart from changing the cumbersome input string, which I can unfortunately not).