Search This Blog

Sunday, January 24, 2021

Console app that Formats JSON string

Console application that prints formatted JSON from a json string. "JValue.Parse(jsonString).ToString(Formatting.Indented)" can be used in you web application as well.


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace ConsoleAppTryOut
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonString = "[{\"Question\":{\"QuestionId\":49,\"QuestionText\":\"Whats your name?\",\"Answer\":\"xyz\"}},{\"Question\":{\"QuestionId\":51,\"QuestionText\":\"Are you smart?\",\"Answer\":\"Yes\"}}]";

            Console.WriteLine(JValue.Parse(jsonString).ToString(Formatting.Indented));

            Console.Read();
        }
    }
}


The formatted output looks as below



No comments:

Post a Comment