2

A web service is sending me a JSON string. It contains a valid JSON string, but it is poorly formatted. Is there a way I can format it to better display it on the screen?

I was hoping to apply something like this:

JsonSerializerOptions() { WriteIndented = true }

, but I can't find the API to do it... Any advice? Thanks.

dbc
  • 104,963
  • 20
  • 228
  • 340
Stout
  • 463
  • 8
  • 19
  • Can you show us your json pls? And what parser are you using? – Serge Sep 19 '22 at 18:02
  • "I can't find the API to do it" - what do you mean by this? I think most if not all JSON serializers you'll find supports writing indented JSON. Where did you look? – Xerillio Sep 19 '22 at 18:04
  • @Serge: I just need to add the spacing, indentation, and newline characters: "{\"Sound\":true,\"Volume\":80,\"Indexing\":true,\"Settings\":[{\"Item1\":{\"Key\":1,\"Value\":1},\"Item2\":1,\"Item3\":0,\"Item4\":1,\"Item5\":0,\"Item6\":-1,\"Item7\":false},{\"Item1\":{\"Key\":1,\"Value\":2},\"Item2\":2,\"Item3\":0,\"Item4\":1,\"Item5\":1,\"Item6\":2,\"Item7\":true},{\"Item1\":{\"Key\":3,\"Value\":2},\"Item2\":1,\"Item3\":0,\"Item4\":2,\"Item5\":2,\"Item6\":3,\"Item7\":false},{\"Item1\":{\"Key\":3,\"Value\":3},\"Item2\":1,\"Item3\":0,\"Item4\":1,\"Item5\":0,\"Item6\":4,\"Item7\":false]}} – Stout Sep 19 '22 at 18:11
  • @Xerillio: I'm looking into System.Text.Json.JsonSerializer. The static class has APIs for Deserialize() and Serialize(), but nothing quick about ApplyOptions()... I thought of deserializing then serializing back using the correct options, but I don't have the class type to deserialize into. – Stout Sep 19 '22 at 18:13
  • 2
    If you have some JSON string and need to prettify it using System.Text.Json, you can use `public static string JsonPrettify(this string json)` from [this answer](https://stackoverflow.com/a/67928315/3744182) by [dlumpp](https://stackoverflow.com/users/6753705/dlumpp) to [How to pretty print using System.Text.Json for unknown object](https://stackoverflow.com/q/65620631/3744182). Does that answer your question? – dbc Sep 19 '22 at 18:18
  • @dbc: Yes, thank you, JsonPrettify() is exactly what I need. Can you please post it as the answer, that I can mark it? – Stout Sep 19 '22 at 18:26

2 Answers2

3

For your json you can use just one line of the code

  1. Using new JsonNode
string formattedJson = System.Text.Json.Nodes.JsonNode.Parse(json).ToString();
  1. Using traditional JsonDocument
string formattedJson = System.Text.Json.JsonSerializer
.Serialize( JsonDocument.Parse(json), 
new JsonSerializerOptions() { WriteIndented = true });

In both cases result is the same

{
  "Sound": true,
  "Volume": 80,
  "Indexing": true,
  "Settings": [
    {
      "Item1": {
        "Key": 1,
        "Value": 1
      },
      "Item2": 1,
      "Item3": 0,
      "Item4": 1,
      "Item5": 0,
      "Item6": -1,
      "Item7": false
    },
    {
      "Item1": {
        "Key": 1,
        "Value": 2
      },
      "Item2": 2,
      "Item3": 0,
      "Item4": 1,
      "Item5": 1,
      "Item6": 2,
      "Item7": true
    },
    {
      "Item1": {
        "Key": 3,
        "Value": 2
      },
      "Item2": 1,
      "Item3": 0,
      "Item4": 2,
      "Item5": 2,
      "Item6": 3,
      "Item7": false
    },
    {
      "Item1": {
        "Key": 3,
        "Value": 3
      },
      "Item2": 1,
      "Item3": 0,
      "Item4": 1,
      "Item5": 0,
      "Item6": 4,
      "Item7": false
    }
  ]
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Rather than reopening a question that was already closed as a duplicate with the consent of the querent, you should add your answer to the canonical question. – dbc Sep 19 '22 at 18:47
  • This is correct, but may have worse performance than [the accepted answer](https://stackoverflow.com/a/67928315/3744182) to [How to pretty print using System.Text.Json for unknown object](https://stackoverflow.com/q/65620631/3744182). Checking the [source code](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonNode.Parse.cs#L68), `JsonNode.Parse()` calls `JsonElement.ParseValue` and then builds the node tree from that. That is likely to be slower than working directly with `JsonDocument` and/or `JsonElement`. – dbc Sep 19 '22 at 18:58
  • @dbc I am sorry, but this is just my variant for the OP json. I am not offering it as a generic solution. Performance difference would be not noticeable for this json . But if the performance is very important, OP is free to look for another solution. I think I will consider your suggestion about posting the new answers in the previous questions – Serge Sep 19 '22 at 19:30
0

var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(object, Formatting.Indented);

could work, but could really use more information.