-1

I have a json data like

{"Player1": {"inventory": {"tas-count": 5, "kiriktas-count": 0, "odun-count": 0}}}

But it seems too complex. I want to edit, change it like

{
  "Player1": {
    "inventory": {
      "tas-count": 5,
      "kiriktas-count": 0,
      "odun-count": 0,
    }
  }
}

I looked for it but there is nothing on Stackoverflow and also things like "\n" are not working. I heard that in other languages, there are libraries for making a clear json data. Might there are some like this in Python.

Nurqm
  • 4,715
  • 2
  • 11
  • 35

2 Answers2

2

You can try:

import json
data = {"Player1": {"inventory": {"tas-count": 5, "kiriktas-count": 0, "odun-count": 0}}}

print(json.dumps(data, indent=4, sort_keys=True))

Output:

{
    "Player1": {
        "inventory": {
            "kiriktas-count": 0,
            "odun-count": 0,
            "tas-count": 5
        }
    }
}
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
2

Here's an example:

>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
    "4": 5,
    "6": 7
}

You can check out pretty printing here: https://docs.python.org/3/library/json.html

Rorepio
  • 332
  • 1
  • 4
  • 16