I'm new to C#, I'm assigned a work to build an API Test application. The Responses are in JSON format(complex), I have to validate(test) the responses for it's correct format, correct type of variables, Check Whether Mandatory datas' are filled etc dynamically(without explicitly creating classes to store deserialized values). Please help me through it.
-
To validate your JSON format, you can use this http://jsonlint.com/ – coderblogger Mar 08 '16 at 10:02
-
@avantvous , I don't want to check the syntactic validation rather validate each and every property (values). Basically I want to parse the JSON dynamically and validate those. – Abhishek Gowda S Mar 08 '16 at 10:14
-
@avantvous does JSON Lint expose an API point to invoke? I understand Abhishek needs automated approach towards testing API (JSON texts). – Siva Senthil Mar 08 '16 at 10:36
-
@SivaSenthil no, jsonlint just check the format of the JSON string. – coderblogger Mar 08 '16 at 10:37
2 Answers
If you are testing against an integration environment / live API, you could use a framework called RestFluencing.
Rest.GetFromUrl("https://api.github.com/users/defunkt")
.WithHeader("User-Agent", "RestFluencing Sample")
.Response()
.ReturnsDynamic(c => c.login == "defunkt", "Login did not match")
.ReturnsDynamic(c => c.id == 2, "ID did not match")
.Assert();
Also if you have a class model you could use an JsonSchema validator:
public class GitHubUser
{
public int id { get; set; }
public string login { get; set; }
}
Validating:
Rest.GetFromUrl("https://api.github.com/users/defunkt")
.Response()
.HasJsonSchema<GitHubUser>(new JSchemaGenerator())
.Assert();
Check out the GitHub page: RestFluencing
Disclaimer: I am the dev for that framework. Doing an full integration api testing in an easy way in C# has been something that I have been looking for a long time, hence this Fluent-style framework.

- 308
- 2
- 8
@Abhishek, System.Json is a .NET Framework namespace which could fulfill your needs. Do explore this namespace in MSDN here - https://msdn.microsoft.com/en-us/library/system.json(v=vs.110).aspx.
In this (How to make sure that string is Valid JSON using JSON.NET) post one more alternative - JSON.NET and the way to use it is discussed.I would still recommend using System.Json until there is a compelling reason to use 3rd party API (JSON.NET).
To validate the data type and the range of values in a property consider JsonType property of JsonValue class. This property returns an enumeration which you could use to validate the data type. For date data type, you will have to parse it from the string using DateTime.Parse method.

- 1
- 1

- 610
- 6
- 22