c#: Get json from JObject without formatting which contains extra spaces and carriage returns

moondaddy 911 Reputation points
2021-02-04T21:53:27.173+00:00

Using c# and .net 4.8 I need to combine 2 json strings. For this I'm using the JObject, but if you have a better (more efficient/faster) way to do this, please let know. this will run on the server so efficient is important.

This is what I'm doing now:

string json1 = "{\"Header1\":[[[\"id\",\"b2a30048-fdd3-490d-a8c2-0da023349aa1\"],[\"name\",\"A Cool Name\"],[\"rating\",3.3],[\"timeStamp\",\"2021-02-04T15:45:39.0844116-06:00\"]]]}";
string json2 = "{\"Header2\":[[[\"id\",\"b2a30048-fdd3-490d-a8c2-0da023349aa1\"],[\"name\",\"A Cool Name\"],[\"rating\",3.3],[\"timeStamp\",\"2021-02-04T15:45:39.0844116-06:00\"]]]}";

JObject test1 = JObject.Parse(json1);
JObject test2 = JObject.Parse(json2);

var result = new JObject();
result.Merge(test1);
result.Merge(test2);

Console.WriteLine(result);

As you can see, the json strings are compact text. However, result.ToString() looks like this:

{
  "Header1": [
    [
      [
        "id",
        "b2a30048-fdd3-490d-a8c2-0da023349aa1"
      ],
      [
        "name",
        "A Cool Name"
      ],
      [
        "rating",
        3.3
      ],
      [
        "timeStamp",
        "2021-02-04T15:45:39.0844116-06:00"
      ]
    ]
  ],
  "Header2": [
    [
      [
        "id",
        "b2a30048-fdd3-490d-a8c2-0da023349aa1"
      ],
      [
        "name",
        "A Cool Name"
      ],
      [
        "rating",
        3.3
      ],
      [
        "timeStamp",
        "2021-02-04T15:45:39.0844116-06:00"
      ]
    ]
  ]
}

and if this was a large json it would be very bloated for sending across the wire.

Please let me know the most efficient way to do this getting compact results.

Thank you.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points
    2021-02-05T01:38:47.097+00:00

    Replace line 11 with this

            Console.WriteLine(result.ToString(Formatting.None));
    

    I see this as the output

    {"Header1":[[["id","b2a30048-fdd3-490d-a8c2-0da023349aa1"],["name","A Cool Name"],["rating",3.3],["timeStamp","2021-02-04T16:45:39.0844116-05:00"]]],"Header2":[[["id","b2a30048-fdd3-490d-a8c2-0da023349aa1"],["name","A Cool Name"],["rating",3.3],["timeStamp","2021-02-04T16:45:39.0844116-05:00"]]]}

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-02-05T02:17:20.323+00:00

    In JSON, these characters don't seem to affect the efficiency of execution, the difference is only visually different.

    Are whitespace characters insignificant in JSON?

    But anyway, if you don't like these characters, you can use the String.Replace Method to replace them with empty strings.

    Console.WriteLine(re.Replace("\r\n","");  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.