2

I'm struggling to make this conversion happen, and not sure it's entirely feasible. My JSON from a third party could look like this:

{
"equipments": [
    {
        "serialNumber": "11-17-053",
        "equipmentType_id": "589dda4952172110008870c7",
        "created": 1508856453875,
        "fieldOffice_id": "594af5425fbfca00111a0c20",
        "clients_id": [],
        "notes": "",
        "isInService": true,
        "metavalues": {
            "0t78nzhp9w265avlvt": {
                "item_ids": [
                    33121
                ]
            },
            "7ogz4kehqh8h3cwip8": {
                "item_ids": [
                    33128
                ]
            }
        },
        "schedules": [],
        "id": "59ef52854d40a9009c787596"
    },
    {
        "serialNumber": "11-17-054",
        "equipmentType_id": "589dda4952172110008870c7",
        "created": 1508856453875,
        "fieldOffice_id": "594af5425fbfca00111a0c20",
        "clients_id": [],
        "notes": "",
        "isInService": true,
        "metavalues": {
            "0t78nzhp9w265avlvt": {
                "item_ids": [
                    33121
                ]
            },
            "7ogz4kehqh8h3cwip8": {
                "item_ids": [
                    33128
                ]
            }
        },
        "schedules": [],
        "id": "59ef52854d40a9009c787597"
    },
    {
        "serialNumber": "8-17-022",
        "equipmentType_id": "589dda4952172110008870c7",
        "created": 1505326964589,
        "fieldOffice_id": "594af5425fbfca00111a0c20",
        "clients_id": [],
        "notes": "",
        "isInService": true,
        "metavalues": {
            "0t78nzhp9w265avlvt": {
                "item_ids": [
                    33121
                ]
            },
            "7ogz4kehqh8h3cwip8": {
                "item_ids": [
                    33128
                ]
            }
        },
        "schedules": [],
        "id": "59b9777480e426009d01d48d"
    },
    {
        "serialNumber": "22A-17-001",
        "equipmentType_id": "589dda4952172110008870c7",
        "created": 1504908025733,
        "fieldOffice_id": "58b74b080c206710004ff726",
        "clients_id": [
            "59bbfdf5725cd00012fb15d8"
        ],
        "notes": "",
        "isInService": true,
        "metavalues": {
            "0t78nzhp9w265avlvt": {
                "item_ids": [
                    33122
                ]
            },
            "7ogz4kehqh8h3cwip8": {
                "item_ids": [
                    33128
                ]
            },
            "g99idmcqyuo2na9e6l": "YARD",
            "709pm94prt2tpjt90y": 5,
            "bgen1h4i5b6f8xa1kh": "9/8/2017 7:18:24 PM",
            "yvtvsl8dedudqjtdud": "0.00000, 0.00000",
            "aj3h2b5fdbic9s72m3": "Parex",
            "8wt1re82xidjiv8rzi": "YARD"
        },
        "schedules": [],
        "id": "59b312f93256d5009c4a73fb"
    },....

This is obviously not a complete example, but should help get my question across.

Is there a way to create a C# class that pulls in certain fields only if they exist from the "metavalues" array?

My current class is as follows which works to get the data, but not exactly how I want.

public class Equipment
{
    [JsonProperty("serialNumber")]
    public string SerialNumber { get; set; }
    public string equipmentType_id { get; set; }
    public bool isInService { get; set; }
    public List<string> clients_Id { get; set; }
    public string fieldOffice_id { get; set; }
    [JsonProperty("id")]
    public string EquipmentId { get; set; }
    [JsonProperty("metavalues")]
    public Dictionary<string, object> metavalues { get; set; } 
}

What I'm after, is taking the key of "g99idmcqyuo2na9e6l" which is optional in the metavalues, and store it in a property called "LeaseName".

I tried the following to no avail.

    public class Equipment
    {
        [JsonProperty("serialNumber")]
        public string SerialNumber { get; set; }
        public string equipmentType_id { get; set; }
        public bool isInService { get; set; }
        public List<string> clients_Id { get; set; }
        public string fieldOffice_id { get; set; }
        [JsonProperty("id")]
        public string EquipmentId { get; set; }
        [JsonProperty("g99idmcqyuo2na9e6l")]
        public string LeaseName { get; set; }
    }

If I try to make a class for the metavalues section, I get an exception indicating that JSON.NET can't convert it to my object, hence why I used the Dictionary<string, object> option.

EDIT # 1: The accepted answer works for me, but for those who stumble upon this and truly need a property name from a nested array you could try the following.

[OnDeserialized]
        private void OnDeserialized(StreamingContext context)
        {
            if (metavalues != null)
            {
                if (metavalues.ContainsKey("g99idmcqyuo2na9e6l"))
                {
                    string _LeaseName = (string)metavalues["g99idmcqyuo2na9e6l"];
                    LeaseName = _LeaseName;
                }
            }
        }

I think my edit approach is a bit overkill but just throwing this out there.

Chris Fischer
  • 161
  • 10

1 Answers1

3

Yes, this is possible, but since the lease value is still one level down in the JSON you need an intermediate class to hold it (to replace the dictionary).

public class Equipment
{
    ...
    [JsonProperty("metavalues")]
    public MetaValues MetaValues { get; set; }
}

public class MetaValues
{
    [JsonProperty("g99idmcqyuo2na9e6l")]
    public string LeaseName { get; set; }
}

Fiddle: https://dotnetfiddle.net/Ddqzc7

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • I made an edit just for further information on a different approach but I like your answer better for sure. – Chris Fischer Nov 14 '17 at 17:00
  • @ChrisFischer - you could also use `JsonPathConverter` from [Can I specify a path in an attribute to map a property in my class to a child property in my JSON?](https://stackoverflow.com/a/33094930)? – dbc Nov 14 '17 at 23:54