Is there a better way to parse json document and extract property value of a particular node in C#(.net core), without knowing root property's name? Given: - root of json document will have only one property but property name is unknown. Would prefer to have a solution without using NewtonSoft library but instead System.Text.JSon
Sample json document below:
{
"rootPropertyName": {
"someProperty1": "TCC",
"someProperty2": "128-1600-8routeextent",
"eventType": "RouteUpdated",
---leaving other properties for sake of brevity---
}
}
As seen above, I would like to parse above json document to extract a particular node's value say "eventType" (this property name is known to me) but I don't know the root property name.
Current implementation:
using JsonDocument doc = JsonDocument.Parse(jsonContent);
{
JsonElement root = doc.RootElement;
string rootPropertyName = root.EnumerateObject().First().Name;
var eventType = root.GetProperty(rootPropertyName).GetProperty("eventType").GetString();
var payload = root.GetProperty(rootPropertyName).ToString();
return (eventType, payload);
}