Skip to content

How to pretty print JSON using .Display() #3718

Closed Answered by jonsequitur
NikiforovAll asked this question in Q&A
Discussion options

You must be logged in to vote

Which JSON?

Code highlighting is a feature of VS Code and it will show up as long as the MIME type has a corresponding notebook renderer.

Indentation is a feature of various JSON serializers. Here's a simple method to indent some JSON using System.Text.Json:

    private static string Indent(string json)
    {
        json = JsonNode.Parse(json).ToJsonString(new JsonSerializerOptions
        {
            WriteIndented = true,
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        });
        return json;
    }

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@NikiforovAll
Comment options

@jonsequitur
Comment options

Answer selected by NikiforovAll
@NikiforovAll
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants