Search This Blog

Showing posts with label Format JSON string. Show all posts
Showing posts with label Format JSON string. Show all posts

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