1

I am currently facing a challenge with deserializing a JSON object returned from Umbraco CMS that contains dynamic fields.

The json object returned from Umbraco is highly customizable which may have examples like this.

{
"content": {
        "DataType": null,
        "header": {
            "title": "",
            "logo": null,
            "navigation": []
        },
        "footer": {
            "Name" : "this is the footer"
            "logo": null,
            "Links": 0.0,
            "copyRight": ""
        }
}

or something more complicated like

{ 
    "overview": "<h2>Content Overview</h2><p><a href=\"https://\">Text</a></p>",
                "isVisible": false,
                "description": "describe your product",
                "bulletItems": [
     "settings": null,
     "content":{
        "item": "confidential service",
        "contentType": {
          "key": "123",
          "id": 1111,
          "alias": "item",
          "itemType": "Element",
          "properties": [
             {
               "referenceCacheLevel": "Element",
               "propertyType": {
                  "contentType": {
                                "key": "3234234",
                                "id": 1112,
                                "alias": "bulletItem",
                                "itemType": "Element",
                                "compositionAliases": [],
                                "variations": "Nothing",
                                "propertyTypes": [],
                                "isElement": true
                    }
                }
             }
          ]
        }
     }
    ]
}

Specifically, the received object may or may not include fields such as header, footer, icon, link, title, content, and so on. My goal is to deserialize this object and fit it into a standard structure (the structure covers the fields, arrays with objects which we are required). If it has the property in my class (same name), then deserialize it and fill in the fields. If it does not have the related properties, then leave it empty. In essence, the imported JSON object will serve as a data source, and the desired outcome will be an object that adheres to the standardized structure with all the necessary elements filtered accordingly.

For instance, here is the structure I have defined:

public class MyContentClass
{
  public Header header;
  public Footer footer;
  public string title;
  ... 
}

public class Header 
{
   public string name;
   public int height; 
   public List<property> properties;
   ...
}

public class Footer 
{
   public string name;
   public string content1;
   public string content2;
   public List<property> properties;
   ... 
}

...

Any advice/suggestion will be highly appreciated.

Rich
  • 313
  • 1
  • 2
  • 10
  • 1
    You need at least to show the json samples. – Guru Stron Jul 14 '23 at 10:49
  • 1
    *"object returned from Umbraco CMS that contains dynamic fields."* - you could always deserialize it to `Dictionary`, some [examples](https://stackoverflow.com/a/58521511). or be a bit strict and complicated by introducing your own custom deserializer. – Bagus Tesa Jul 14 '23 at 10:49
  • @GuruStron Added samples that could be highly customized. There is no fixed format. – Rich Jul 15 '23 at 13:22

1 Answers1

0

You can archieve this via using System.Text.Json.

Refer the below code snippet:

using System;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        // Pass you json string or MyContentClass objects json string
        string jsonString = "{\"name\":\"John\",\"age\":30,\"Prop\":40,\"city\":\"New York\"}";

        // Deserialize the JSON string into a dynamic object
        dynamic dynamicObject = JsonSerializer.Deserialize<dynamic>(jsonString);
        Console.WriteLine(""+ dynamicObject.ToString());

    }
}

Refer the working sample here.

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42
  • _"My goal is to deserialize this object and fit it into a standard structure"_ - Your code does not achieve the claimed goal. – Guru Stron Jul 14 '23 at 11:52