How to Validate a JSON File in C# .NET Framework
1 min readJul 20, 2020
JSON validation is an incredibly useful feature to have around. Many languages will treat invalid JSONs as null, or try to parse them in some very strange ways, causing some major problems. It is best to simply weed out invalid JSONs beforehand and deal with it before it becomes an issue. Rather than try to write this yourself, I am going to show you how to apply an API to the problem, getting you results very fast, indeed.
First we download our package using NuGet:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8
Following that we call our function:
using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ValidateDocumentJsonValidationExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ValidateDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Validate a JSON fileDocumentValidationResult result = apiInstance.ValidateDocumentJsonValidation(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ValidateDocumentApi.ValidateDocumentJsonValidation: " + e.Message );}}}}
And we are officially finished! Now you have JSON validation, no problem.