Click here to Skip to main content
15,887,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my API testing I am using Jcontainer to Convert response to Json.
Ex:
C#
[Test]
public void GetUsersList()
{
    var response = us.UserList();
    JContainer jsonresponse = rh.ConvertResponseToJson(response);
}



I am trying to the following validation against the Json
Verify if all Keys are present (If all keys in json are present, like id, timestamp, type etc..)


Here is my json
[
  {
    "id": "aa0db615-d4cb-4466-bc23-0e0083002330",
    "timestamp": "2020-02-11T19:00:00-05:00",
    "type": 33554432,
    "info": "Full Synchronization request for all endpoints",
    "schedule": "once",
    "lastRun": null,
    "flags": 6,
    "creator": null,
    "isEditable": true,
    "location": 0,
    "duration": null
  },
  {
    "id": "70baa28c-e270-447b-b88a-20d30a9542db",
    "timestamp": "2020-02-11T19:00:00-05:00",
    "type": 33554432,
    "info": "Full Synchronization request for all endpoints",
    "schedule": "once",
    "lastRun": null,
    "flags": 6,
    "creator": null,
    "isEditable": true,
    "location": 0,
    "duration": null
  },



Here is my Convert respone to Json for reference 
<pre> public JContainer ConvertResponseToJson(HttpWebResponse response)
        {
            string localString;

            if (response.ContentEncoding.Contains("application/xml"))
            {
                // Convert the escaped Stream into an XML document.
                ConfigXmlDocument xmlDocument = new ConfigXmlDocument();
                xmlDocument.LoadXml(ConvertResponseStreamToString(response));

                // Now convert the properly-escaped JSON for the response into a JContainer
                localString = JsonConvert.SerializeXmlNode(xmlDocument);
            }
            else
                localString = ConvertResponseStreamToString(response);

            return JToken.Parse(localString) as JContainer;
        }


What I have tried:

For now I created a model of the Json to read it by array index. But I am doing mutiple assetions to vaidate all keys. I want to just loop through them. Here is what i have so far
var response = us.UserList();
    JContainer jsonresponse = rh.ConvertResponseToJson(response);
 var castedModel = Jsonresponse.ToObject<IList<Model>>();
            Assert.IsNotNull(castedModel[0].info);  //This is repeated I am trying to avoid this
          Assert.IsNotNull(castedModel[0].task);
           Assert.IsNotNull(castedModel[0].timestamp)
Posted
Updated 30-Mar-20 9:25am
v5
Comments
Richard MacCutchan 30-Mar-20 3:32am    
What is the question?
Member 14779968 30-Mar-20 13:13pm    
I am trying to do the following validation against the Json response.

1. Validate all keys are present in the Json.
ex: If response contains id, timestamp, type, info etc.. Trying to iterate through Json to achieve this.

1 solution

Assuming that the question is in the name getting value of the property is as simple as this
C#
var sut = ConvertResponseToJson(req);
if (sut is JArray)
{
	var firstElementId = ((JArray)sut)[0]["id"];                
} 
else
{
	var elementId = sut["id"];
}

Note that your response might be an array as in the example so I check it.
If you want to get all the keys of any array's item
C#
var items = ((JArray)sut).Children();
foreach (JObject child in items)
{
	var allKeys = child.Properties().Select(p => p.Name).ToList();
}


Although I think the most elegant solution would be to create a class which will represent a schema of the expected JSON, deserialize your response and verify it
 
Share this answer
 
Comments
Member 14779968 30-Mar-20 13:02pm    
Hi Bohdan, Yes I updated the question as to what I am trying to accomplish here. I created a model class presenting the Json. But Instead of using mutiple assertions to validate every keys, I want to loop through the json and validate if all the keys are present

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