[Python] In JSON output, force every opening curly brace to appear in a new separate line

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    With json.dumps(some_dict,indent=4,sort_keys=True) in my code:

    I get something like this:

    {
    "a": {
    "x":1,
    "y":2
    },
    "b": {
    "z":3,
    "w":4
    }
    }


    But I want something like this:

    {
    "a":
    {
    "x":1,
    "y":2
    },
    "b":
    {
    "z":3,
    "w":4
    }
    }


    How can I force each opening curly brace to appear at the beginning of a new separate line?

    Do I have to write my own JSON serializer, or is there a special argument that I can use when calling json.dumps?

    Continue reading...

Compartilhe esta Página