How to covert the below json
{"data":{"id":12,"name":"jeremy","email":"[email protected]"}}
to
{"id":12,"name":"jeremy","email":"[email protected]"}
I want to remove the "data" element from json.
With json.net it's fairly straightforward
var input = "{"data":{"id":12,"name":"jeremy","email":"[email protected]"}}"; var result = JObject.Parse(input)["data"].ToString(Formatting.None); Console.WriteLine(result);
Note : Formatting.None is only to preserve the formatting you had in your original example
Formatting.None
Or Text.Json
var result = JsonDocument.Parse(input).RootElement.GetProperty("data").ToString();
Output
Additional Resources
JObject.Parse Method (String)
Load a JObject from a string that contains JSON.
JObject.Item Property (String)
Gets or sets the JToken with the specified property name.
JToken.ToString Method (Formatting,JsonConverter[])
Returns the JSON for this token using the given formatting and converters.
Formatting Enumeration
None 0 No special formatting is applied.
Text.Json
JsonDocument.Parse Method
Provides a mechanism for examining the structural content of a JSON value without automatically instantiating data values.
JsonDocument.RootElement Property
Gets the root element of this JSON document
JsonElement.GetProperty Method
Gets a JsonElement representing the value of a required property identified by propertyName.
2.1m questions
2.1m answers
60 comments
57.0k users