2

When writing a json string to a file using the following code below, it doesn't show up in a pretty format when editing the file, but rather just shows up as a string format. If I use .Net fiddle, the console output is correct and it looks good. How do I write the json string to a file, so when editing /w say notepad++ it shows up in a pretty format. Can someone help?

.Net Fiddle code for console output which works fine

public class Program
{
    public static void Main()
    {
        String sPrettyStr;
        var item = "{\"messageType\":\"0\",\"code\":\"1\"}";
        sPrettyStr = JValue.Parse(item).ToString(Newtonsoft.Json.Formatting.Indented);
        Console.WriteLine(sPrettyStr);
    }
}

.Net Fiddle console output

{
  "messageType": "0",
  "code": "1"
}

But when writing a formatted json string to file using this

File.WriteAllText(c:/test.json,JValue.Parse(item).ToString(Newtonsoft.Json.Formatting.Indented)) 

I get this file output when edited w/ notepad++

"{\\\"messageType\\\":\\\"0\\\",\\\"code\\\"}"
  • 3
    Possible duplicate of [How do I get formatted JSON in .NET using C#?](https://stackoverflow.com/questions/2661063/how-do-i-get-formatted-json-in-net-using-c) – Tobias Theel Feb 21 '19 at 21:45
  • Nope, I've tried all those examples and they don't work. The file output still remains in a string format. –  Feb 21 '19 at 21:53
  • So using this [link](https://www.newtonsoft.com/json/help/html/WriteJsonWithJsonTextWriter.htm) does not work? – HenryMigo Feb 21 '19 at 21:55
  • 3
    I've copied and pasted your code into LINQPad and it produces the output you want, so there is apparently some code you're not showing that is interfering with the code. – Heretic Monkey Feb 21 '19 at 22:01
  • 2
    I copy/pasted your code into a new console app, and got the desired results as well with no changes. – GalacticJello Feb 21 '19 at 22:04
  • Same for me, just tried it. The code produces the expexted, pretty printed result. See [here](https://imgur.com/a/DbxHgAc) – Tobias Theel Feb 21 '19 at 22:06
  • 3
    Try using JToken.Parse() instead of JValue.Parse(). Maybe you are using older version of the library that treats given string as a literal. – zendu Feb 21 '19 at 22:08
  • The JToken.Parse() seems to work and now the File output is in the correct format. Thanks so much! –  Feb 21 '19 at 22:23

2 Answers2

4

After having a gander and attempting myself this is what I found: You first have to deserialize the string then serialize it again.

EDIT: Took your code as well and got the desired output also like others in comment have said. I tweaked to use JToken instead of JValue and all is good.

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

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            var item = "{\"messageType\":\"0\",\"code\":\"1\"}";

            var t = JsonConvert.DeserializeObject(item);
            var x = JsonConvert.SerializeObject(t, Formatting.Indented);

            string sPrettyStr;
            var item2 = "{\"messageType\":\"0\",\"code\":\"1\"}";
            sPrettyStr = JToken.Parse(item2).ToString(Formatting.Indented);

            Console.WriteLine(x);
            Console.WriteLine(sPrettyStr);
            Console.ReadLine();
        }
    }
}

Credit to Frank from here

HenryMigo
  • 164
  • 1
  • 7
0

Try this code:

var item = "{\"messageType\":\"0\",\"code\":\"1\"}";
JToken token = JToken.Parse(item);
JObject json = JObject.Parse((string) token);
Console.WriteLine(json);
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
djsp
  • 19
  • 2