2

In Unity, I save my game using JSON files. When I open the file in Visual Studio, it shows the whole content with all its variables in one single line. Here is a small part of my JSON file:

// JSON before my copy / paste-trick (see below)
{"lastTicketDate":"04.13.2020","lastTicket":{"Id":2,"Type":0,"Fortune":"Fortune02","Author":"Me!!!\r","ShinyEffect":0,"BeenDrawn":true},"firstStart":false,"tickets":[{"Id":1,"Type":0,"Fortune":"Fortune01","Author":
// (...)

This is not very readable. How can I set up Visual Studio to show me the content properly, each variable in a separate line, like this:

// JSON after my copy / paste trick (see below)
{
    "lastTicketDate": "04.13.2020",
    "lastTicket": {
        "Id": 2,
        "Type": 0,
        "Fortune": "Fortune02",
        "Author": "Me!!!\r",
        "ShinyEffect": 0,
        "BeenDrawn": true
    },
    "firstStart": false,
    "tickets": [
        {
            "Id": 1,
            "Type": 0,
            "Fortune": "Fortune01",
            "Author": "Me!!!\r",
            "ShinyEffect": 0,
            "BeenDrawn": false
        },
// (...)

Currently, I do it like this: Double-click on one word -> copy it (Ctrl+c) -> paste it back in (Ctrl+v) -> now the format changed to the desired version.

How do I fix this issue, what is the correct way to do it?

Bugzzi Snu
  • 35
  • 5
  • 2
    `Ctrl+K, D`?... – GSerg Apr 13 '20 at 15:39
  • 3
    That's Ctrl+K, Ctrl+D in case the previous comment didn't make it clear. It's the keyboard shortcut for "Edit" -> "Format Document". – Andrew Morton Apr 13 '20 at 15:45
  • 2
    After I cleared all bookmarks in all files (Ctrl+B, C), your shortcut works now. But I have to do this every time the JSON file got changed. I was hoping that it would just work, right out of the box. – Bugzzi Snu Apr 13 '20 at 15:47

1 Answers1

4

Right now when saving you output your json in the smallest size format, which is unindented, so that is why everything is so compressed.

You can instead, change your json serializer to output an idented format.

e.g. when using the builtin json util;

https://docs.unity3d.com/ScriptReference/JsonUtility.ToJson.html

var indented = JsonUtility.ToJson(this, true);

EDIT

Since this answer got some traction over time, here's how to output indented JSON in other popular libraries:

Json.NET (Newtonsoft)

JsonConvert.SerializeObject(json, Formatting.Indented);

System.Text.Json

JsonSerializer.Serialize(json, new JsonSerializerOptions { WriteIndented = true })
sommmen
  • 6,570
  • 2
  • 30
  • 51