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

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    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")?

    Continue reading...

Compartilhe esta Página