Click here to Skip to main content
15,443,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new on C# and also not hands on json. I want to deserialization this json data into C# objects but don't know how to deal with these guids "ae7da5d6-4024-4f7d-9221-5d2298afe650": {} in "menus" and "categories". Your help will be appreciate.

What I have tried:

{
    "eventId": "",
    "eventTime": "2022-03-08T07:35:33Z",
    "eventType": "",
    "metadata": {
        "storeId": "",
        "applicationId": "",
        "resourceId": null,
        "payload": {
            "menuData": {
                "menus": {
                    "ae7da5d6-4024-4f7d-9221-5d2298afe650": {
                        "name": "POSIGENT-DEV",
                        "categoryIds": ["e6860d33-8626-4ef0-0358-08d7df7bade0", "fe3bfb19-ed73-4ae4-0359-08d7df7bade0", "1e53b53b-ee16-4205-035a-08d7df7bade0"],
                        "fulfillmentModes": ["DELIVERY", "DINE_IN", "PICK_UP"],
                        "id": "ae7da5d6-4024-4f7d-9221-5d2298afe650",
                        "hoursData": {
                            "timeZone": "GMT",
                            "regularHours": [],
                            "specialHours": []
                        }
                    }
                },
                "categories": {

                    "e6860d33-8626-4ef0-0358-08d7df7bade0": {
                        "name": "Soup",
                        "description": "",
                        "itemIds": ["c30bfa7f-042f-4297-e46a-08d7df94661c", "b516b115-a3e3-4723-3b66-08d81902cd59", "b0e9784b-9a95-4ec7-c645-08d81d8e0aa6", "c3af2a3a-e653-4f71-c649-08d81d8e0aa6", "1854763b-3ea7-48e9-799f-08d8bd29d0f1"],
                        "id": "e6860d33-8626-4ef0-0358-08d7df7bade0"
                    },
                    "fe3bfb19-ed73-4ae4-0359-08d7df7bade0": {
                        "name": "Main Course",
                        "description": "",
                        "itemIds": ["b02b2e78-4d33-480b-ae0a-08d7df7ca760", "55e4d1ed-29ec-472e-e466-08d7df94661c"],
                        "id": "fe3bfb19-ed73-4ae4-0359-08d7df7bade0"
                    },
                    "d59853d1-ff64-496e-3d39-08d7fa4d3d0c": {
                        "name": "Italian Cuisine",
                        "description": "",
                        "itemIds": ["f812490e-0598-480f-dabf-08d7fa4f2954", "ee06d429-0a98-44e5-4ee7-08d7fa77824b"],
                        "id": "d59853d1-ff64-496e-3d39-08d7fa4d3d0c"
                    },
                    "650910ed-39f5-499b-74f9-08d7fbd34052": {
                        "name": "Pizza",
                        "description": "",
                        "itemIds": ["20152ec6-b728-4f9e-1ecc-08d7fdb8106f", "00146279-b5c7-4f0e-cbf6-08d8019c8287", "cea3ed3e-76bd-4bc9-5926-08d811f841d3"],
                        "id": "650910ed-39f5-499b-74f9-08d7fbd34052"
                    },
                    "fc500654-fcdf-492b-7864-08d81110c0b6": {
                        "name": "kids menu",
                        "description": "",
                        "itemIds": ["1a6747b3-26fc-4cfe-7b27-08d81110f844"],
                        "id": "fc500654-fcdf-492b-7864-08d81110c0b6"
                    },
                    "e70efee8-ed50-4a65-c778-08d85b0fb03a": {
                        "name": "Beverages",
                        "description": "",
                        "itemIds": ["af91f3f4-b902-4e25-e102-08d85e437b29", "38eaa51d-cf50-4f61-3688-08d870eb7628"],
                        "id": "e70efee8-ed50-4a65-c778-08d85b0fb03a"
                    }}}}}}
Posted
Updated 9-Mar-22 23:44pm

This CodeProject article might give you some clues: Working with JSON in C# & VB[^]

It is also possible to use: Visual Studio - Edit - Paste Special - Paste JSON As Classes
but that probably will not generate what you want :)
 
Share this answer
 
v2
While C# supports GUID's JSON doesn't - they are represented by strings instead.

However, that isn't technically valid JSON: the string to the left of a colon character is a field name, not a value, so your JSON example treats each new GUID value as a separate class, and that's a problem:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class HoursData
    {
        public string timeZone { get; set; }
        public List<object> regularHours { get; set; }
        public List<object> specialHours { get; set; }
    }

    public class Ae7da5d640244f7d92215d2298afe650
    {
        public string name { get; set; }
        public List<string> categoryIds { get; set; }
        public List<string> fulfillmentModes { get; set; }
        public string id { get; set; }
        public HoursData hoursData { get; set; }
    }

    public class Menus
    {
        [JsonProperty("ae7da5d6-4024-4f7d-9221-5d2298afe650")]
        public Ae7da5d640244f7d92215d2298afe650 Ae7da5d640244f7d92215d2298afe650 { get; set; }
    }

    public class E6860d3386264ef0035808d7df7bade0
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class Fe3bfb19Ed734ae4035908d7df7bade0
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class D59853d1Ff64496e3d3908d7fa4d3d0c
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class _650910ed39f5499b74f908d7fbd34052
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class Fc500654Fcdf492b786408d81110c0b6
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class E70efee8Ed504a65C77808d85b0fb03a
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class Categories
    {
        [JsonProperty("e6860d33-8626-4ef0-0358-08d7df7bade0")]
        public E6860d3386264ef0035808d7df7bade0 E6860d3386264ef0035808d7df7bade0 { get; set; }

        [JsonProperty("fe3bfb19-ed73-4ae4-0359-08d7df7bade0")]
        public Fe3bfb19Ed734ae4035908d7df7bade0 Fe3bfb19Ed734ae4035908d7df7bade0 { get; set; }

        [JsonProperty("d59853d1-ff64-496e-3d39-08d7fa4d3d0c")]
        public D59853d1Ff64496e3d3908d7fa4d3d0c D59853d1Ff64496e3d3908d7fa4d3d0c { get; set; }

        [JsonProperty("650910ed-39f5-499b-74f9-08d7fbd34052")]
        public _650910ed39f5499b74f908d7fbd34052 _650910ed39f5499b74f908d7fbd34052 { get; set; }

        [JsonProperty("fc500654-fcdf-492b-7864-08d81110c0b6")]
        public Fc500654Fcdf492b786408d81110c0b6 Fc500654Fcdf492b786408d81110c0b6 { get; set; }

        [JsonProperty("e70efee8-ed50-4a65-c778-08d85b0fb03a")]
        public E70efee8Ed504a65C77808d85b0fb03a E70efee8Ed504a65C77808d85b0fb03a { get; set; }
    }

    public class MenuData
    {
        public Menus menus { get; set; }
        public Categories categories { get; set; }
    }

    public class Payload
    {
        public MenuData menuData { get; set; }
    }

    public class Metadata
    {
        public string storeId { get; set; }
        public string applicationId { get; set; }
        public object resourceId { get; set; }
        public Payload payload { get; set; }
    }

    public class Root
    {
        public string eventId { get; set; }
        public DateTime eventTime { get; set; }
        public string eventType { get; set; }
        public Metadata metadata { get; set; }
    }
I would suspect that you need to go back to where you got that from as it's not right - the GUID value is duplicated as the field name and the ID within the field.
Your JSON should look like this:
{
    "eventId": "",
    "eventTime": "2022-03-08T07:35:33Z",
    "eventType": "",
    "metadata": {
        "storeId": "",
        "applicationId": "",
        "resourceId": null,
        "payload": {
            "menuData": {
                "menus": {
                    "MyClassWithCategories": {
                        "name": "POSIGENT-DEV",
                        "categoryIds": ["e6860d33-8626-4ef0-0358-08d7df7bade0", "fe3bfb19-ed73-4ae4-0359-08d7df7bade0", "1e53b53b-ee16-4205-035a-08d7df7bade0"],
                        "fulfillmentModes": ["DELIVERY", "DINE_IN", "PICK_UP"],
                        "id": "ae7da5d6-4024-4f7d-9221-5d2298afe650",
                        "hoursData": {
                            "timeZone": "GMT",
                            "regularHours": [],
                            "specialHours": []
                        }
                    }
                },
                "categories": {

                    "MyCategories": {
                        "name": "Soup",
                        "description": "",
                        "itemIds": ["c30bfa7f-042f-4297-e46a-08d7df94661c", "b516b115-a3e3-4723-3b66-08d81902cd59", "b0e9784b-9a95-4ec7-c645-08d81d8e0aa6", "c3af2a3a-e653-4f71-c649-08d81d8e0aa6", "1854763b-3ea7-48e9-799f-08d8bd29d0f1"],
                        "id": "e6860d33-8626-4ef0-0358-08d7df7bade0"
                    },
                    "MyCategories": {
                        "name": "Main Course",
                        "description": "",
                        "itemIds": ["b02b2e78-4d33-480b-ae0a-08d7df7ca760", "55e4d1ed-29ec-472e-e466-08d7df94661c"],
                        "id": "fe3bfb19-ed73-4ae4-0359-08d7df7bade0"
                    },
                    "MyCategories": {
                        "name": "Italian Cuisine",
                        "description": "",
                        "itemIds": ["f812490e-0598-480f-dabf-08d7fa4f2954", "ee06d429-0a98-44e5-4ee7-08d7fa77824b"],
                        "id": "d59853d1-ff64-496e-3d39-08d7fa4d3d0c"
                    },
                    "MyCategories": {
                        "name": "Pizza",
                        "description": "",
                        "itemIds": ["20152ec6-b728-4f9e-1ecc-08d7fdb8106f", "00146279-b5c7-4f0e-cbf6-08d8019c8287", "cea3ed3e-76bd-4bc9-5926-08d811f841d3"],
                        "id": "650910ed-39f5-499b-74f9-08d7fbd34052"
                    },
                    "MyCategories": {
                        "name": "kids menu",
                        "description": "",
                        "itemIds": ["1a6747b3-26fc-4cfe-7b27-08d81110f844"],
                        "id": "fc500654-fcdf-492b-7864-08d81110c0b6"
                    },
                    "MyCategories": {
                        "name": "Beverages",
                        "description": "",
                        "itemIds": ["af91f3f4-b902-4e25-e102-08d85e437b29", "38eaa51d-cf50-4f61-3688-08d870eb7628"],
                        "id": "e70efee8-ed50-4a65-c778-08d85b0fb03a"
                    }}}}}}
Which produces much more sensible classes:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class HoursData
    {
        public string timeZone { get; set; }
        public List<object> regularHours { get; set; }
        public List<object> specialHours { get; set; }
    }

    public class MyClassWithCategories
    {
        public string name { get; set; }
        public List<string> categoryIds { get; set; }
        public List<string> fulfillmentModes { get; set; }
        public string id { get; set; }
        public HoursData hoursData { get; set; }
    }

    public class Menus
    {
        public MyClassWithCategories MyClassWithCategories { get; set; }
    }

    public class MyCategories
    {
        public string name { get; set; }
        public string description { get; set; }
        public List<string> itemIds { get; set; }
        public string id { get; set; }
    }

    public class Categories
    {
        public MyCategories MyCategories { get; set; }
    }

    public class MenuData
    {
        public Menus menus { get; set; }
        public Categories categories { get; set; }
    }

    public class Payload
    {
        public MenuData menuData { get; set; }
    }

    public class Metadata
    {
        public string storeId { get; set; }
        public string applicationId { get; set; }
        public object resourceId { get; set; }
        public Payload payload { get; set; }
    }

    public class Root
    {
        public string eventId { get; set; }
        public DateTime eventTime { get; set; }
        public string eventType { get; set; }
        public Metadata metadata { get; set; }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900