1

I have JSON with parts in Unicode like { "val1": "\u003c=AA+ \u003e=AA-"} How can I convert this to JSON which does not have Unicode formatting? {"val1": "<=AA+ >=AA-"}

phuclv
  • 37,963
  • 15
  • 156
  • 475
Anand
  • 1,387
  • 2
  • 26
  • 48

2 Answers2

0

Json.NET unescapes Unicode sequences inside JsonTextReader, so you can adopt the same approach as is used in this answer to How do I get formatted JSON in .NET using C#? by Duncan Smart to reformat your JSON without unnecessary escaping by streaming directly from a JsonTextReader to a JsonTextWriter using JsonWriter.WriteToken(JsonReader):

public static partial class JsonExtensions
{
    // Adapted from this answer https://stackoverflow.com/a/30329731
    // To https://stackoverflow.com/q/2661063
    // By Duncan Smart https://stackoverflow.com/users/1278/duncan-smart

    public static string JsonPrettify(string json, Formatting formatting = Formatting.Indented)
    {
        using (var stringReader = new StringReader(json))
        using (var stringWriter = new StringWriter())
        {
            return JsonPrettify(stringReader, stringWriter, formatting).ToString();
        }
    }

    public static TextWriter JsonPrettify(TextReader textReader, TextWriter textWriter, Formatting formatting = Formatting.Indented)
    {
        // Let caller who allocated the the incoming readers and writers dispose them also
        // Disable date recognition since we're just reformatting
        using (var jsonReader = new JsonTextReader(textReader) { DateParseHandling = DateParseHandling.None, CloseInput = false })
        using (var jsonWriter = new JsonTextWriter(textWriter) { Formatting = formatting, CloseOutput = false })
        {
            jsonWriter.WriteToken(jsonReader);
        }
        return textWriter;
    }
}

Using this method, the following code:

var json = @"{ ""val1"": ""\u003c=AA+ \u003e=AA-""}";
var unescapedJson = JsonExtensions.JsonPrettify(json, Formatting.None);
Console.WriteLine("Unescaped JSON: {0}", unescapedJson);

Outputs

Unescaped JSON: {"val1":"<=AA+ >=AA-"}

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340
-1

I tried the following in Linqpad and it worked.

var s = @"{ ""val1"": ""\u003c=AA+ \u003e=AA-""}";
System.Text.RegularExpressions.Regex.Unescape(s).Dump();
Anand
  • 1,387
  • 2
  • 26
  • 48
  • 2
    Unfortunately, this also unescapes the characters that are **required** to be escaped by the [JSON standard](https://json.org/), \ `/` `\"` `\b` `\n` `\r` `\t`, thereby generating corrupt JSON. See https://dotnetfiddle.net/AnBcfO. The basic problem here is that you're using a regex parser to parse JSON, and the grammar, though similar, is not identical. – dbc Jan 24 '19 at 21:31