Search code examples
c#jsondeserializationjson-deserialization

How do I deserialize a JSON array(flat) and ignore some token


I have this response from server

 [{
  "sys_id": "******************************",
  "dv_model_id": "*****************",
  "due": "YYYY-MM-DD HH:mm:ss",
  "assigned_to": "1524s32a54dss412s121s",
  "dv_assigned_to": "username",
  "assigned_to.phone": "+12345678910",
  "assigned_to.email": "abc@a.c",
  "u_borrower_id": "fb36e45f0a12452004742183457e833b0",
  "dv_u_borrower_id": "antoherUserName",
  "u_borrower_id.phone": "+12345678910",
  "u_borrower_id.email": "abcf@a.c"
}
,{....}
,{....}]

I'm trying to deserialize this to List

public class Inventory
{
    public Inventory()
    {
        assigned_to = new User();
        u_borrower_wwid = new User();
    }

    public string sys_ID { get; set; }
    public string dv_model_id { get; set; }
    public DateTime due { get; set; }
    public string dv_assigned_to { get; set; }
    public User assigned_to { get; set; }
    public string dv_u_borrower_id { get; set; }
    public User u_borrower_id { get; set; }
}

now, since the JSON contains - "assigned_to": "1524s32a54dss412s121s"," the deserialization failed. the same with the - ""u_borrower_id": "fb36e45f0a12452004742183457e833b0"," .

do you know any way to ignore them? or remove them from the JSON? I need only the properties (".phone" and ".email") of the object.

any ideas?


Solution

  • I see a number of solutions:


    Modify your Inventory object (or create a new one) so the json can be fully deserialized, then access the values there.

    Your new and updated object should look like this:

    public class InventoryJsonObject
    {
        public string sys_id { get; set; }
        public string dv_model_id { get; set; }
        public string due { get; set; }
        public string assigned_to { get; set; }
        public string dv_assigned_to { get; set; }
    
        [JsonProperty("assigned_to.phone")]
        public string assigned_to_phone { get; set; }
    
        [JsonProperty("assigned_to.email")]
        public string assigned_to_email { get; set; }
        public string u_borrower_id { get; set; }
        public string dv_u_borrower_id { get; set; }
    
        [JsonProperty("u_borrower_id.phone")]
        public string u_borrower_id_phone { get; set; }
    
        [JsonProperty("u_borrower_id.email")]
        public string u_borrower_id_email { get; set; }
    }
    

    Use regular expressions to get the values from the string. In this case your regex would be "u_borrower_id\.phone": "(.*?)" and "u_borrower_id\.email": "(.*?)"

    The complete regex solution could look like this (assuming every object has a phone and email included):

    string phonePattern = "\"u_borrower_id\\.phone\": \"(.*?)\"";
    string emailPattern = "\"u_borrower_id\\.email\": \"(.*?)\"";
    
    Regex phoneRegex = new Regex(phonePattern);
    var phoneMatches = phoneRegex.Matches(input);
    
    Regex emailRegex = new Regex(emailPattern);
    var emailMatches = emailRegex.Matches(input);
    
    for (int i = 0; i < phoneMatches.Count; i++)
    {
        string phoneMatch = phoneMatches[i].Groups[1].Value;
        string emailMatch = emailMatches[i].Groups[1].Value;
        // Now you can add them to any collection you desire
    }
    

    Implement a cast between string and User. Since your error originates from the fact that the string fb36e45f0a12452004742183457e833b0 cannot be cast into a User object trivially, you have to implement the cast. It would look like this:

    public static implicit operator User(string _string)
    {
        // This could be a DB lookup, or basically anything else
        return new User()
        {
            id = _string
        };
    }