Display content of JSON files in multiple lines, instead of one long line
Asked Answered
M

2

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?

Moraine answered 13/4, 2020 at 15:36 Comment(3)
Ctrl+K, D?...Phlebitis
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".Xeres
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.Moraine
E
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 })
Eucaine answered 13/4, 2020 at 15:48 Comment(2)
This actually did the trick! Thank you and thank you for the link to the doc~Moraine
Glad i could help!Eucaine
A
0

I have used this tool to format JSON from one line to multiple lines as you described and its quite handy. https://sdetools.io/formatter/json-formatter

It allows me to encode and provide different indentation options (Tab, 2 spaces, 3 spaces, etc).

Archibald answered 22/6, 2024 at 14:38 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewPyrolysis

© 2022 - 2025 — McMap. All rights reserved.