Deserialize JSON and assert response

Viewed 261

Essentially I'm trying to write a test to deserialize the JSON response to the Customer object and Assert that the Firstname is "Darron"

JSON object

  {
        "Id": 615,
        "Customer": {
            "Id": 726,
            "MemberNodeId": 2257,
            "EmailAddress": "Darron_Lips@email.com",
            "Deleted": false,
            "LastModifiedBy": "CUSTOMER",
            "LastModifiedOn": "2021-06-02T08:54:38.243Z",
            "CreatedBy": "CUSTOMER",
            "CreatedOn": "2021-06-02T08:54:38.243Z"
        },
        "Title": {
            "Id": 1,
            "Title": "Mr"
        },
        "Firstname": "Darron",
        "Lastname": "Lips",
        "Gender": "Male",
        "DateOfBirth": "1977-11-21T00:00:00Z",
        "Height": 241.000000000,
        "Weight": 204.000000000,
        "LastModifiedBy": "CUSTOMER",
        "LastModifiedOn": "2021-06-02T08:54:38.257Z",
        "CreatedBy": "CUSTOMER",
        "CreatedOn": "2021-06-02T08:54:38.257Z"
    }
]

Customer model

   public class Customer
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("MemberNodeId")]
        public int MemberNodeId { get; set; }

        [JsonProperty("EmailAddress")]
        public string EmailAddress { get; set; }

        [JsonProperty("Deleted")]
        public bool Deleted { get; set; }

        [JsonProperty("LastModifiedBy")]
        public string LastModifiedBy { get; set; }

        [JsonProperty("LastModifiedOn")]
        public DateTime LastModifiedOn { get; set; }

        [JsonProperty("CreatedBy")]
        public string CreatedBy { get; set; }

        [JsonProperty("CreatedOn")]
        public DateTime CreatedOn { get; set; }
    }

    public class CustomerTitle
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Title")]
        public string Title { get; set; }
    }

    public class Root
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Customer")]
        public Customer Customer { get; set; }

        [JsonProperty("Title")]
        public string Title { get; set; }

        [JsonProperty("Firstname")]
        public string Firstname { get; set; }

        [JsonProperty("Lastname")]
        public string Lastname { get; set; }

        [JsonProperty("Gender")]
        public string Gender { get; set; }

        [JsonProperty("DateOfBirth")]
        public DateTime DateOfBirth { get; set; }

        [JsonProperty("Height")]
        public int Height { get; set; }

        [JsonProperty("Weight")]
        public int Weight { get; set; }

        [JsonProperty("LastModifiedBy")]
        public object LastModifiedBy { get; set; }

        [JsonProperty("LastModifiedOn")]
        public DateTime LastModifiedOn { get; set; }

        [JsonProperty("CreatedBy")]
        public string CreatedBy { get; set; }

        [JsonProperty("CreatedOn")]
        public DateTime CreatedOn { get; set; }
    }

}

and the Test

  [Test]
        public void CanGetCustomerDetails()
        {
            RestClient client = new RestClient(baseUrl);
            RestRequest request = new RestRequest("CustomerDetailsApi/GetAll", Method.GET);
            request.AddHeader("Authorization", $"Bearer {token}");
            IRestResponse response = client.Execute(request);

       
        }

so far I have tried

  var customer = client.Execute(request).Content;
        var responsea = JsonConvert.DeserializeObject<List<Customer>>(customer);
        Assert.AreEqual(responsea, "firstname");

and

   var customer = client.Execute(request).Content;
            var responsea = JsonConvert.DeserializeObject<Customer>(customer);

which results in:

"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type "

2 Answers

Your JSON doesn't match your classes. First of all, you should be deserialising into List<Root>, but you also need to modify that class to have a child class for the Title property. Also, those numbers for Height and Weight cannot be deserialised into int properties.

So first of all, is your Root class:

public class Root
{
    public int Id { get; set; }
    public Customer Customer { get; set; }
    public CustomerTitle Title { get; set; } // Correct type used here
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Gender { get; set; }
    public DateTime DateOfBirth { get; set; }
    public double Height { get; set; } // double rather than int
    public double Weight { get; set; } // double rather than int
    public object LastModifiedBy { get; set; }
    public DateTime LastModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
}

Now deserialise into correct type:

var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);

Now you can assert something like this:

Assert.AreEqual(responsea[0].Firstname, "Darron");

Note that you don't need all the JsonProperty attributes if the property names already match.

Try this:

  var customer = client.Execute(request).Content;
        var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);
        Assert.AreEqual(responsea[0].Firstname , "Darron");

Essentially responsea[0].Firstname access the first element of the List take the Firstname property of it and assert with the value, which seems to be "Darron"

Your second try, essentially tries to convert an entire array to one object which is raises the exception.

Moreover, you have some declared types that do not match your JSON.

Related