-1

I'M building Api that need to return json array as response like this :

"tagList": ["dragons", "training"],

But I got ((each element in separate line )):

"tagList": [
        "dragons",
        "training"
    ],

I was using System.Text.Json.Serialization; I tried to change it to Json.net by JsonConvert.SerializeObject(obj) but I got the same result . could anyone help me ,please ?

dbc
  • 104,963
  • 20
  • 228
  • 340
Rawan
  • 17
  • 5
  • With Json.NET you can, check the accepted answer here https://stackoverflow.com/questions/53223517/creating-json-without-array-indentation – Romka Jun 10 '22 at 09:34
  • 1
    The two snippets are *identical*. Whitespace isn't significant in JSON. Why do you want the output in a single line? It matters. Are you trying to return streaming JSON aka JSON per line aka newline-delimited JSON perhaps? – Panagiotis Kanavos Jun 10 '22 at 09:52
  • Are you asking how to format a JSON array on a single line **while the containing JSON is formatted on multiple lines** for cosmetic reasons? If so, this is not possible with System.Text.Json, see [In System.Text.Json is it possible to specify custom indentation rules?](https://stackoverflow.com/q/63376873/3744182). With Json.NET it is possible, see the previously linked [Creating JSON without array indentation](https://stackoverflow.com/q/53223517/3744182) as well as [Newtonsoft inline formatting for subelement while serializing](https://stackoverflow.com/q/30831895/3744182). – dbc Jun 10 '22 at 14:11

1 Answers1

0

since you are using System.Text.Json.Serialization , try this

 JsonSerializerOptions options = new JsonSerializerOptions();
  options.WriteIndented = false //not using indent, white space, new line, etc.
    
    string jsonString = JsonSerializer.Serialize(obj, options);
Serge
  • 40,935
  • 4
  • 18
  • 45