Create identically named JSON object and array using python [duplicate]

Create identically named JSON object and array using python [duplicate]
python
Ethan Jackson

I try to achieve the following using the default python encoder:

{ "x": { "name": "value" }, "x": [ { "other_name": "other_value" } ] }

My current JSON skills allow the following

import json data = {} data["x"] = {} data["x"]["name"] = "value" y = {} y["other_name"] = "other_value" data["x"] = [y] print(json.dumps(data))

which of course only results in

{ "x": [ { "other_name": "other_value" } ] }

How can I get an output that contains a JSON object and an identically named array (here "x")?

Answer

I think that in JSON, you cannot have duplicate keys at the same level of an object. that each key in a JSON object must be unique.

Related Articles