Deserialize JSON With Dynamic Properties
I'm trying to build a deserializer for GCP log entries, and they all seem to have the same basic structure, and it looks like our own log info gets stored in the
textPayload
property of the JSON of a log entry, but that's not really relevant.
My problem is many different entries have different structures for their labels
properties. For example, one entry will have labels like this:
and another entry will have labels like this:
I've already figured it's probably best to deserialize the labels
property into a dictionary rather than something vague like object
or daft like dynamic
, but I have no clue on how to go about this.
I have half an idea I must write a converter for the labels
property, but there are other labels
properties nested within properties not shown in these examples, so how do I write a converter for only the labels
property at the root of the log entry json object?
Is a converter even the way to go, or is there an easier way to just deserialize a json property to a to a dictionary, with no real conversions needed?6 Replies
JsonElement Labels { get; set; }
?Thanks, that's a new one for me, but it doesn't seem to provide much access to the content of the element until you deserialize it, but into what type? An anonymous object maybe, that I have to reflect on to get its list of properties.
i don't understand
you use the JsonElement API to read what the object is
All I can see that the jsonElement has ValueKind that seems to be Object right now.
it has many methods on it for reading the values of the properties
you need to look at the documentation and not just the debugger
for example, there is a method called
EnumerateObject
which will let you get all of the key/value pairsThanks man. I had seen that but overlooked it, and will visit it again.