C#C
C#5mo ago
VoidPointer

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",
}

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"
}

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?
Was this page helpful?