-1

I receive a response I have no control over from an API. Using requests response.json() will filter out duplicate keys. So I would need to turn this response into a list where each key is an element in that list: What I get now:

{
  "user": {
    //...
  },
  "user": {
    //...
  },
  //...
}

What I need:

{
  "users": [
   {
    "user": {
      //...
   }
   },
   {
    "user": {
      //...
    }
    },
    //...
  ]
}

This way JSON won't filter out any of the results, and I can loop through users.

Majs
  • 607
  • 2
  • 7
  • 20
  • 1
    Your expected json structure is invalid – bigbounty Jul 09 '20 at 08:03
  • 4
    Does this answer your question? [Python json parser allow duplicate keys](https://stackoverflow.com/questions/29321677/python-json-parser-allow-duplicate-keys) – LiuXiMin Jul 09 '20 at 08:04
  • I see I missed 2 curly brackets, should be valid now. @LiuXiMin no I've checked all there thread and maybe I'm just not experienced enough but that has not helped me answer my question. Not sure why I get downvotes and vote for duplicate when its not a duplicate. The answer in that Q is not the answer to this question. Maybe some of you can use it to figure out the answer to this Q but I cant. – Majs Jul 09 '20 at 14:14
  • @Majs I posted an answer, which should work – LiuXiMin Jul 10 '20 at 01:06

1 Answers1

1

Okay, let me have a try by method used in Python json parser allow duplicate keys

All we should do is handle the pairs_list by ourself.


from json import JSONDecoder


def parse_object_pairs(pairs):
    return pairs


data = """
{"foo": {"key": 2, "key": 3}, "foo": 4, "foo": 23}
"""

decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
pairs_list = decoder.decode(data)

# the pairs_list is the real thing which we can use

aggre_key = 's'

def recusive_handle(pairs_list):
    dct = {}
    for k, v in pairs_list:
        if v and isinstance(v, list) and isinstance(v[0], tuple):
            v = recusive_handle(v)
        if k + aggre_key in dct:
            dct[k + aggre_key].append({k: v})
        elif k in dct:
            first_dict = {k: dct.pop(k)}
            dct[k + aggre_key] = [first_dict, {k: v}]
        else:
            dct[k] = v
    return dct


print(recusive_handle(pairs_list))

output:

{'foos': [{'foo': {'keys': [{'key': 2}, {'key': 3}]}}, {'foo': {'bar': 4}}, {'foo': 23}]}
LiuXiMin
  • 1,225
  • 8
  • 17