2

I have a dictionary with (string, list) as key-value pair (list may be empty), which I write to file using:

with open(out_file, "w") as f:
    json.dump(data, f, indent=4)

If I don't use indent=4, everything is in the same line.

Problem: Output is like

{
    "key_1": [
            1,
            2,
    ],
    "key_2": []
}

while I wanted (mainly because the lists usually have around 10 numbers only, and easier to view on same line):

{
    "key_1": [1, 2],
    "key_2": []
}

Any idea have I can achieve this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ankit Kumar
  • 1,145
  • 9
  • 30
  • 2
    This post maybe also helps you: [https://stackoverflow.com/questions/21866774/pretty-print-json-dumps](https://stackoverflow.com/questions/21866774/pretty-print-json-dumps). – Bram Dekker Jul 02 '20 at 10:30
  • @BramDekker that was precisely to the point, thank you :) – Ankit Kumar Jul 02 '20 at 10:31

0 Answers0