How to Deserialize Json with illegal character(Guid) in property name RRS feed

  • Question

  • User-2128588023 posted

    How to deserialize the below json result?can anyone please help on this,i am not able to create a c# class using http://json2csharp.com/

                    "2189f3b7-de3a-4cd5-868b-ef50e4d3aa46": {
                        "DataTypes": [
                            " DataField",
                        "ChilData": [
                        "Id": "2189f3b7-da3a-4cc5-868b-ef50e4d3aa26",
                        "DisplayName": " test Product",
                        "LocaleName": "test",
                        "ShortName": "test_topic",
                        "LastModifiedByPersonId": "f292b452-5441-e011-9767d8d385dcbb12",
                        "LastModifiedDate": "2014-12-08T22:33:05.551",
                        "ParentId": "00000000-0000-0000-0000-000000000000",
                        "Order": 0,
                        "Level": 0
                    "235caff5-250c-402e-aabc-39ecc3042851": {
                        "DataTypes": [
                        "ChilData": [
                        "Id": "235caff5-250c-402e-aabc-39ecc3042851",
                        "DisplayName": "test Band",
                        "LocaleName": "hin",
                        "ShortName": "test1",
                        "LastModifiedByPersonId": "846ead80-ce6b-464f-9b10-4addb1201ae2",
                        "LastModifiedDate": "2015-10-26T20:46:54.243",
                        "ParentId": "2189f3b7-da3a-4cc5-868b-ef50e4d3aa26",
                        "Order": 0,
                        "Level": 1


    Monday, June 19, 2017 2:38 PM

All replies

  • User-474980206 posted

    the json is valid, but c# is more restrictive in property names than javascript. if the guids are consistent, then a common approach is to replace the "-" with "_". C# also requires a property name start with a letter. so you also need a prefix. edit the Json to have valid C# names and convert. then use the JsonProperty attribute (assuming you are using json.net) to define the correct json property name:


    public TheClassName GUID_2189f3b7_de3a_4cd5_868b_ef50e4d3aa46 {get; set;}

    if the guids change on every request, then you will need to write a custom serializer, and use keyed collection for the guid property names. 

    Monday, June 19, 2017 2:54 PM
  • User-2128588023 posted

    Thanks bruce for quick reply,In my Case the GUIDS are  changing on every request,can you please give any example or link , how to deserialize the same using custom class.

    Monday, June 19, 2017 3:09 PM
  • User1967761114 posted

    Hi Ramakanta090,

    According to your description, I suggest you could use JObject in Newtonsoft.Json, see the following example code .

    var str = "{\"2189f3b7-de3a-4cd5-868b-ef50e4d3aa46\": {\"DataTypes\": [\" DataField\",\"FilterField\"],\"ChilData\": [\"235caff5-250c-402e-aabc-39ecc3042851\",\"481dd7e2-001c-4681-8eae-010af51d957d\"],\"Id\": \"2189f3b7-da3a-4cc5-868b-ef50e4d3aa26\",\"DisplayName\": \" test Product\",\"LocaleName\": \"test\",\"ShortName\": \"test_topic\",\"LastModifiedByPersonId\": \"f292b452-5441-e011-9767d8d385dcbb12\",\"LastModifiedDate\": \"2014-12-08T22:33:05.551\",\"ParentId\": \"00000000-0000-0000-0000-000000000000\",\"Order\": 0,\"Level\": 0},\"235caff5-250c-402e-aabc-39ecc3042851\": {\"DataTypes\": [\"MetaValue\"],\"ChilData\": [\"412bd900-008e-4bde-923b-8f1a5c547e59\"],\"Id\": \"235caff5-250c-402e-aabc-39ecc3042851\",\"DisplayName\": \"test Band\",\"LocaleName\": \"hin\",\"ShortName\": \"test1\",\"LastModifiedByPersonId\": \"846ead80-ce6b-464f-9b10-4addb1201ae2\",\"LastModifiedDate\": \"2015-10-26T20:46:54.243\",\"ParentId\": \"2189f3b7-da3a-4cc5-868b-ef50e4d3aa26\",\"Order\": 0,\"Level\": 1}}";
    var jObject = JObject.Parse(str);
    //enumerable the properties in jObject
    foreach (var item in jObject)
        var dataTypes = item.Value.Value<JArray>("DataTypes");
        var chilData = item.Value.Value<JArray>("ChilData");
        var id = item.Value.Value<string>("Id");
        var lastModifiedDate = item.Value.Value<DateTime>("LastModifiedDate");
    //get value by property name
    var value = jObject.Value<JObject>("2189f3b7-de3a-4cd5-868b-ef50e4d3aa46");
    var types = value.Value<JArray>("DataTypes");

    For more details about how to use JObject, you could refer to the following link.



    If you have any other questions, please feel free to contact me any time.

    Best Regards


    Tuesday, June 20, 2017 2:03 AM