Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

Minifying and Formatting JSON data with C#

Posted on 26 May 2016

Nowadays it’s very common to receive JSON data from many sources and to process it in our programs. I have the same problem and, sometimes, I also have to debug the received data to see what’s coming from the server. Many times, the data is minimized and it’s very difficult to analyze what’s coming. On the other side, I have formatted JSON data and want to save space, minimizing it.

You can go to online sites (just do a search for “json formatter” in your preferred search engine) and format the JSON data, to get the formatted output.

But, as a developer, I wanted to create a program that does that. So I decided to create a Windows UWP program to process the JSON data.

The first step is to create a UWP program in Visual Studio:

In MainPage.xaml, we add the two textboxes, one for the minified JSON and the other for the processed JSON:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="\*"/>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="\*"/>
   </Grid.ColumnDefinitions>
   <TextBox x:Name="MiniBox" Margin="5" AcceptsReturn="True" TextWrapping="Wrap"/>
   <StackPanel Grid.Column="1" VerticalAlignment="Center">
     <Button Width="85" Height="40" Content="Format >>" Margin="5" />
     <Button Width="85" Height="40" Content="<< Minify" Margin="5" />
   </StackPanel>
   <TextBox x:Name="FormBox" Margin="5" Grid.Column="2" AcceptsReturn="True" TextWrapping="Wrap"/>
</Grid>
XML

To process the JSON data, we use the Newtonsoft Json.NET library (http://www.newtonsoft.com/json). We can add it using NuGet. In the Solution Explorer, right click in References and select “Manage NuGet packages” and add the library Newtonsoft.Json.

Then, in MainPage.xaml, add the handler for the Click handler for the Format button:

<Button Width="90" Height="40" Content="Format >>"  Margin="5" Click="FormatJsonClick"/>
XML

Select the Click event handler and press F12 to create the event handler in code and type this code:

private async void FormatJsonClick(object sender, RoutedEventArgs e)
{
    try
    {
        if (!string.IsNullOrWhiteSpace(MiniBox.Text))
        {
            FormBox.Text = JsonConvert.SerializeObject(
                JsonConvert.DeserializeObject(MiniBox.Text), Formatting.Indented);
        }
    }
    catch (JsonReaderException ex)
    {
        await new MessageDialog("Error parsing JSON: {ex.Message}").ShowAsync();
    }
}
C#

We are using two functions of the library to format the data: DeserializeObject, to transform the string into an object and then SerializeObject to transform the object into a string again. This time, we use the Formatting.Indented to format the result and add it to the destination box. To minify the JSON, you must use the same procedure, but use Formatting.None:

private async void MinifyJsonClick(object sender, RoutedEventArgs e)
{
    try
    {
        if (!string.IsNullOrWhiteSpace(FormBox.Text))
        {
            MiniBox.Text = JsonConvert.SerializeObject(
                JsonConvert.DeserializeObject(FormBox.Text), Formatting.None);
        }
    }
    catch (JsonReaderException ex)
    {
        await new MessageDialog("Error parsing JSON: {ex.Message}").ShowAsync();
    }
}
C#

If you run the program and paste some JSON into the first box and click Format, the formatted JSON is shown in the second box.

If you want to reverse the process, just click on the Minify button.

Easy, no? Just one line of code and you have a Minifier/Formatter for JSON data.

If you want to take a look at the source code for the project, you can go to https://github.com/bsonnino/ProcessJson

4 thoughts on “Minifying and Formatting JSON data with C#”

  1. Shailesh says:
    25 June 2020 at 21:02

    Hi Bruno,

    This solves my problem. I was looking at mutiple example of converting the string JSON into formatted json to display on HTML page. I tried couple of example but non of them works for me. This is exactly what i was looking for.
    Thanks for the example.

    Reply
  2. Vikas says:
    28 December 2020 at 15:51

    In Visual Studio if you want to convert JSON into C# Class, You can also navigate to File-> new -> Paste special -> JSON as Class

    For formatting also check
    https://www.minify-beautify.com/json-formatter-online

    Thanks

    Reply
    1. bsonnino says:
      28 December 2020 at 20:04

      Yes, I know that. I will publish soon a post that uses this feature to convert XML to Json

      Reply
  3. Pingback: 2016 Retrospective–Open Source Projects – Bruno Sonnino

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About