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:
{
"textPayload": "xxx",
"timestamp": "xxx",
"severity": "DEBUG",
"labels": {
"python_logger": "xxx",
"environment": "xxx",
"hostInstance": "xxx"
},
"logName": "xxx",
}
{
"textPayload": "xxx",
"timestamp": "xxx",
"severity": "DEBUG",
"labels": {
"python_logger": "xxx",
"environment": "xxx",
"hostInstance": "xxx"
},
"logName": "xxx",
}
and another entry will have labels like this:
{
"textPayload": "xxx",
"timestamp": "xxx",
"severity": "DEBUG",
"labels": {
"api-id": "xxx",
"start-datetime": "xxx",
"Session": "xxx",
"identity-number": "xxx",
"api-url": "xxx",
"segment-group-id": "00000000-0000-0000-0000-000000000000",
"success": "true",
"kyc-id": "xxx",
"latency": "5"
},
"logName": "xxx"
}
{
"textPayload": "xxx",
"timestamp": "xxx",
"severity": "DEBUG",
"labels": {
"api-id": "xxx",
"start-datetime": "xxx",
"Session": "xxx",
"identity-number": "xxx",
"api-url": "xxx",
"segment-group-id": "00000000-0000-0000-0000-000000000000",
"success": "true",
"kyc-id": "xxx",
"latency": "5"
},
"logName": "xxx"
}
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
reflectronic
reflectronic2w ago
JsonElement Labels { get; set; }?
VoidPointer
VoidPointerOP2w ago
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.
reflectronic
reflectronic2w ago
i don't understand you use the JsonElement API to read what the object is
VoidPointer
VoidPointerOP2w ago
All I can see that the jsonElement has ValueKind that seems to be Object right now.
reflectronic
reflectronic2w ago
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 pairs
VoidPointer
VoidPointerOP2w ago
Thanks man. I had seen that but overlooked it, and will visit it again.

Did you find this page helpful?