[Python] How can I suppress newlines within an output array when dumping indented JSON?

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code:

    import json

    my_list = [1, 2, 3]
    my_dict = {"key": "value", "boolean": True}
    my_json = {"object": my_dict, "array": my_list}

    print(json.dumps(my_json, indent=4))


    I get an output like:

    {
    "object": {
    "key": "value",
    "boolean": true
    },
    "array": [
    1,
    2,
    3
    ]
    }


    I want it the elements of the "array" array to appear on the same line, like so:

    {
    "object": {
    "key": "value",
    "boolean": true
    },
    "array": [1, 2, 3]
    }


    How can I get this result?

    Continue reading...

Compartilhe esta Página