323

I am using .NET JSON parser and would like to serialize my config file so it is readable. So instead of:

{"blah":"v", "blah2":"v2"}

I would like something nicer like:

{
    "blah":"v", 
    "blah2":"v2"
}

My code is something like this:

using System.Web.Script.Serialization; 

var ser = new JavaScriptSerializer();
configSz = ser.Serialize(config);
using (var f = (TextWriter)File.CreateText(configFn))
{
    f.WriteLine(configSz);
    f.Close();
}
np_6
  • 504
  • 1
  • 6
  • 19
  • Just for reference: you're not really using "the" .NET JSON parser but rather an old parser created in the old ASP.NET days. Today there's also the new [System.Text.Json](https://docs.microsoft.com/en-us/dotnet/api/system.text.json?view=net-5.0&viewFallbackFrom=netframework-4.8) parser that's way faster and is more considered the out-of-the-box parser to use now with .NET going forward. [JSON.NET](https://www.newtonsoft.com/json) is also another very popular JSON library for .NET. – Jim Aho Jun 15 '21 at 16:45

17 Answers17

303

You are going to have a hard time accomplishing this with JavaScriptSerializer.

Try JSON.Net.

With minor modifications from JSON.Net example

using System;
using Newtonsoft.Json;

namespace JsonPrettyPrint
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Product product = new Product
                {
                    Name = "Apple",
                    Expiry = new DateTime(2008, 12, 28),
                    Price = 3.99M,
                    Sizes = new[] { "Small", "Medium", "Large" }
                };

            string json = JsonConvert.SerializeObject(product, Formatting.Indented);
            Console.WriteLine(json);

            Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
        }
    }

    internal class Product
    {
        public String[] Sizes { get; set; }
        public decimal Price { get; set; }
        public DateTime Expiry { get; set; }
        public string Name { get; set; }
    }
}

Results

{
  "Sizes": [
    "Small",
    "Medium",
    "Large"
  ],
  "Price": 3.99,
  "Expiry": "\/Date(1230447600000-0700)\/",
  "Name": "Apple"
}

Documentation: Serialize an Object

np_6
  • 504
  • 1
  • 6
  • 19
Sky Sanders
  • 34,662
  • 7
  • 68
  • 91
221

A shorter sample code for Json.Net library

private static string FormatJson(string json)
{
    dynamic parsedJson = JsonConvert.DeserializeObject(json);
    return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
Josh Kodroff
  • 26,051
  • 26
  • 92
  • 147
dvdmn
  • 6,038
  • 7
  • 42
  • 51
149

If you have a JSON string and want to "prettify" it, but don't want to serialise it to and from a known C# type then the following does the trick (using JSON.NET):

using System;
using System.IO;
using Newtonsoft.Json;

class JsonUtil
{
    public static string JsonPrettify(string json)
    {
        using (var stringReader = new StringReader(json))
        using (var stringWriter = new StringWriter())
        {
            var jsonReader = new JsonTextReader(stringReader);
            var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
            jsonWriter.WriteToken(jsonReader);
            return stringWriter.ToString();
        }
    }
}
Ekevoo
  • 2,674
  • 1
  • 22
  • 35
Duncan Smart
  • 29,624
  • 8
  • 63
  • 70
121

Shortest version to prettify existing JSON: (edit: using JSON.net)

JToken.Parse("mystring").ToString()

Input:

{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}

Output:

{
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}

To pretty-print an object:

JToken.FromObject(myObject).ToString()
np_6
  • 504
  • 1
  • 6
  • 19
asherber
  • 2,308
  • 1
  • 14
  • 11
  • 4
    This works even without knowing the structure of the json in advance. And it is the shortest answer here。 – foresightyj Dec 29 '16 at 10:32
  • 3
    This works, but only if the json object isn't an array. If you know it'll be an array you could us JArray.Parse instead. – Luke Z Jun 08 '17 at 23:26
  • 6
    Ah, good point, thanks. I've updated my answer to use `JToken` instead of `JObject`. This works with objects or arrays, since `JToken` is the ancestor class for both `JObject` and `JArray`. – asherber Jun 10 '17 at 01:41
  • 1
    Thanks a lot, man I wasted around 2 hours to get to this solution... Can't imagine my life without @stackoverflow ... – Rudresha Parameshappa Aug 18 '17 at 09:26
  • I really prefer this one over the other answers. Short code and effective. Thank you – Marc Roussel Nov 29 '18 at 19:41
  • Will not work for: var json = @"{""dateMicrosoft"":""/Date(1526256000000)/""}"; Outputs: { "dateMicrosoft": "2018-05-14T00:00:00Z" } Havent found a way around this. – Raj Rao Mar 01 '20 at 01:30
61

Oneliner using Newtonsoft.Json.Linq:

string prettyJson = JToken.Parse(uglyJsonString).ToString(Formatting.Indented);
Jay Walker
  • 4,576
  • 5
  • 46
  • 51
Dariusz
  • 13,798
  • 7
  • 50
  • 66
32

All this can be done in one simple line:

string jsonString = JsonConvert.SerializeObject(yourObject, Formatting.Indented);
EvilDr
  • 8,131
  • 11
  • 66
  • 121
Ebube
  • 445
  • 4
  • 5
23

Net Core App

var js = JsonSerializer.Serialize(obj, new JsonSerializerOptions {
             WriteIndented = true
         });
ΩmegaMan
  • 26,526
  • 10
  • 91
  • 107
Sith2021
  • 2,584
  • 25
  • 22
19

Here is a solution using Microsoft's System.Text.Json library:

static string FormatJsonText(string jsonString)
{
    using var doc = JsonDocument.Parse(
        jsonString,
        new JsonDocumentOptions
        {
            AllowTrailingCommas = true
        }
    );
    MemoryStream memoryStream = new MemoryStream();
    using (
        var utf8JsonWriter = new Utf8JsonWriter(
            memoryStream,
            new JsonWriterOptions
            {
                Indented = true
            }
        )
    )
    {
        doc.WriteTo(utf8JsonWriter);
    }
    return new System.Text.UTF8Encoding()
        .GetString(memoryStream.ToArray());
}
Andrew Shepherd
  • 42,538
  • 29
  • 134
  • 197
13

You may use following standard method for getting formatted Json

JsonReaderWriterFactory.CreateJsonWriter(Stream stream, Encoding encoding, bool ownsStream, bool indent, string indentChars)

Only set "indent==true"

Try something like this

    public readonly DataContractJsonSerializerSettings Settings = 
            new DataContractJsonSerializerSettings
            { UseSimpleDictionaryFormat = true };

    public void Keep<TValue>(TValue item, string path)
    {
        try
        {
            using (var stream = File.Open(path, FileMode.Create))
            {
                //var currentCulture = Thread.CurrentThread.CurrentCulture;
                //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

                try
                {
                    using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
                        stream, Encoding.UTF8, true, true, "  "))
                    {
                        var serializer = new DataContractJsonSerializer(type, Settings);
                        serializer.WriteObject(writer, item);
                        writer.Flush();
                    }
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.ToString());
                }
                finally
                {
                    //Thread.CurrentThread.CurrentCulture = currentCulture;
                }
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.ToString());
        }
    }

Pay your attention to lines

    var currentCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    ....
    Thread.CurrentThread.CurrentCulture = currentCulture;

For some kinds of xml-serializers you should use InvariantCulture to avoid exception during deserialization on the computers with different Regional settings. For example, invalid format of double or DateTime sometimes cause them.

For deserializing

    public TValue Revive<TValue>(string path, params object[] constructorArgs)
    {
        try
        {
            using (var stream = File.OpenRead(path))
            {
                //var currentCulture = Thread.CurrentThread.CurrentCulture;
                //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

                try
                {
                    var serializer = new DataContractJsonSerializer(type, Settings);
                    var item = (TValue) serializer.ReadObject(stream);
                    if (Equals(item, null)) throw new Exception();
                    return item;
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.ToString());
                    return (TValue) Activator.CreateInstance(type, constructorArgs);
                }
                finally
                {
                    //Thread.CurrentThread.CurrentCulture = currentCulture;
                }
            }
        }
        catch
        {
            return (TValue) Activator.CreateInstance(typeof (TValue), constructorArgs);
        }
    }

Thanks!

Makeman
  • 684
  • 8
  • 12
  • Hi, @Makeman, have you ever reproduced serialization errors caused by different cultures? Seems like XmlJsonWriter/Reader conversions are all culture invariant. – Olexander Ivanitskyi Sep 30 '19 at 13:30
  • Hello, I am not sure about XmlJsonWriter/Reader, but DataContractJsonSerializer uses Thread.CurrentThread.CurrentCulture. Errors may occur when data have been serialized on the machine A but deserialized on the B with another regional settings. – Makeman Oct 01 '19 at 08:36
  • I decompiled `DataContractJsonSerializer` in assembly `System.Runtime.Serialization v.4.0.0.0`, there is no explicit usage of `CurrentCulture`. The only usage of a culture is `CultureInfo.InvariantCulture` in the base class `XmlObjectSerializer`, internal method `TryAddLineInfo`. – Olexander Ivanitskyi Oct 02 '19 at 12:14
  • So, maybe it is my mistake. I will check it later. Possible, I am extrapolate this culture issue from implementation of an another serializer. – Makeman Oct 04 '19 at 10:12
  • 1
    I have edited the original answer. Seems that DataContract serializers are culture independed, but you should save attention to avoid culture specific errors during serialization by another sorts of serializers. :) – Makeman Oct 14 '19 at 11:30
6

Using System.Text.Json set JsonSerializerOptions.WriteIndented = true:

JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize<Type>(object, options);
Jamie Kitson
  • 3,820
  • 4
  • 32
  • 48
2

First I wanted to add comment under Duncan Smart post, but unfortunately I have not got enough reputation yet to leave comments. So I will try it here.

I just want to warn about side effects.

JsonTextReader internally parses json into typed JTokens and then serialises them back.

For example if your original JSON was

{ "double":0.00002, "date":"\/Date(1198908717056)\/"}

After prettify you get

{ 
    "double":2E-05,
    "date": "2007-12-29T06:11:57.056Z"
}

Of course both json string are equivalent and will deserialize to structurally equal objects, but if you need to preserve original string values, you need to take this into concideration

np_6
  • 504
  • 1
  • 6
  • 19
Max Venediktov
  • 382
  • 2
  • 8
  • There is a great discussion about this detail here ... https://github.com/JamesNK/Newtonsoft.Json/issues/862 Interesting how this detail has evolved. I learned something new about my primary json parser - Thank you for your comment. – Sql Surfer Jun 06 '18 at 20:12
2
using System.Text.Json;
...
var parsedJson = JsonSerializer.Deserialize<ExpandoObject>(json);
var options = new JsonSerializerOptions() { WriteIndented = true };
return JsonSerializer.Serialize(parsedJson, options);
Yola
  • 17,545
  • 11
  • 60
  • 100
1

This worked for me. In case someone is looking for a VB.NET version.

@imports System
@imports System.IO
@imports Newtonsoft.Json
    
Public Shared Function JsonPrettify(ByVal json As String) As String
  Using stringReader = New StringReader(json)

    Using stringWriter = New StringWriter()
      Dim jsonReader = New JsonTextReader(stringReader)
      Dim jsonWriter = New JsonTextWriter(stringWriter) With {
          .Formatting = Formatting.Indented
      }
      jsonWriter.WriteToken(jsonReader)
      Return stringWriter.ToString()
    End Using
  End Using
End Function
np_6
  • 504
  • 1
  • 6
  • 19
Deedz
  • 127
  • 1
  • 10
0

Below code works for me:

JsonConvert.SerializeObject(JToken.Parse(yourobj.ToString()))
newuser
  • 297
  • 2
  • 22
0

For UTF8 encoded JSON file using .NET Core 3.1, I was finally able to use JsonDocument based upon this information from Microsoft: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to#utf8jsonreader-utf8jsonwriter-and-jsondocument

string allLinesAsOneString = string.Empty;
string [] lines = File.ReadAllLines(filename, Encoding.UTF8);
foreach(var line in lines)
    allLinesAsOneString += line;

JsonDocument jd = JsonDocument.Parse(Encoding.UTF8.GetBytes(allLinesAsOneString));
var writer = new Utf8JsonWriter(Console.OpenStandardOutput(), new JsonWriterOptions
{
    Indented = true
});
JsonElement root = jd.RootElement;
if( root.ValueKind == JsonValueKind.Object )
{
    writer.WriteStartObject();
}
foreach (var jp in root.EnumerateObject())
    jp.WriteTo(writer);
writer.WriteEndObject();

writer.Flush();
DrBB
  • 93
  • 5
0

I have something very simple for this. You can put as input really any object to be converted into json with a format:

private static string GetJson<T> (T json)
{
    return JsonConvert.SerializeObject(json, Formatting.Indented);
}
Andrés Fg
  • 108
  • 7
0

.NET 5 has built in classes for handling JSON parsing, serialization, deserialization under System.Text.Json namespace. Below is an example of a serializer which converts a .NET object to a JSON string,

using System.Text.Json;
using System.Text.Json.Serialization;

private string ConvertJsonString(object obj)
{
    JsonSerializerOptions options = new JsonSerializerOptions();
    options.WriteIndented = true; //Pretty print using indent, white space, new line, etc.
    options.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals; //Allow NANs
    string jsonString = JsonSerializer.Serialize(obj, options);
    return jsonString;
}
Nemo
  • 3,023
  • 1
  • 26
  • 22