I've searched for a simple native way to beautify a JSON in C# and have fond none, so I did my own and share it here, hope it helps ;)
It transforms condensed JSON like this:
[{"level":1,"exp":110,"energyGranted":5,"itemGranted":null},{"level":2,"exp":195,"energyGranted":5,"itemGranted":null},{"level":3,"exp":296,"energyGranted":5,"itemGranted":null}]
To nice formated JSON like this:
[
{
"level": 1,
"exp": 110,
"energyGranted": 5,
"itemGranted": null
},
{
"level": 2,
"exp": 195,
"energyGranted": 5,
"itemGranted": null
},
{
"level": 3,
"exp": 296,
"energyGranted": 5,
"itemGranted": null
}
]
Context : I was working on a Unity project that handles JSON responses I get from a backend. I didn't want to append some third party C# library just for that simple task.
There is the JsonUtility.ToJson(obj, prettyPrint)
Unity built-in method doing that but only from an object, not a JSON, thus my need.