How to validate JSON using C# - Schema validation using Newton soft?

DEEPAK KUMPALA 191 Reputation points
2022-09-30T05:40:21.117+00:00

I have below JSON, and would like to make sure atleast one device present under "Devices" node.

{  
  "pageNumber": 1,  
  "pageSize": 250,   
  "data": [  
    {  
      "Id": "6817662d",  
      "LastModified": "2022-04-09T06:40:40+00:00",  
      "IsActive": true        
      "Devices": [  
        {  
          "Id": "d58f034b",            
          "IsActive": true,  
          "SerialNumber": "754"          
        },  
  {  
          "Id": "005056963bac",            
          "IsActive": true,  
          "SerialNumber": "707"    
        }  
      ]  
 }  
}  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2022-09-30T09:34:56.483+00:00

    There is Json.NET Schema for performing validation.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2022-09-30T08:50:24.517+00:00

    Hi @DEEPAK KUMPALA ,

    You can use Deserialize subsections of a JSON payload to finish it.

    I test it under. Net6.0.

    Below is the code:

    using System.Collections.Generic;  
    using System;  
    using System.Text.Json;  
      
    namespace DeserializeExtra  
    {  
    	public class Rootobject  
    	{  
    		public int pageNumber { get; set; }  
    		public int pageSize { get; set; }  
    		public Datum[] data { get; set; }  
    	}  
      
    	public class Datum  
    	{  
    		public string Id { get; set; }  
    		public DateTime LastModified { get; set; }  
    		public bool IsActive { get; set; }  
    		public Device[] Devices { get; set; }  
    	}  
      
    	public class Device  
    	{  
    		public string Id { get; set; }  
    		public bool IsActive { get; set; }  
    		public string SerialNumber { get; set; }  
    	}  
      
    	public class Program  
    	{  
    		public static void Main()  
    		{  
    			string jsonString =  
    @" {  
     	""pageNumber"": 1,  
     	""pageSize"": 250,  
     	""data"": [{  
     		""Id"": ""6817662d"",  
     		""LastModified"": ""2022-04-09T06:40:40+00:00"",  
     		""IsActive"": true,  
     		""Devices"": [{  
     				""Id"": ""d58f034b"",  
     				""IsActive"": true,  
     				""SerialNumber"": ""754""  
     			},  
     			{  
     				""Id"": ""005056963bac"",  
     				""IsActive"": true,  
     				""SerialNumber"": ""707""  
     			}  
     		]  
     	}]  
     }  
    ";  
      
    			Rootobject? rootobject =  
    				JsonSerializer.Deserialize<Rootobject>(jsonString);  
    			  
    			foreach (var datum in rootobject.data)  
    			{  
    				if (datum.Devices != null) {  
    					Console.WriteLine("It at least one device present under Devices node");  
    				}  
    				foreach (var device in datum.Devices)  
    				{  
    					Console.WriteLine($"Id:{device.Id}");  
    				}  
    			}  
    		}  
    	}  
    }  
    

    Output:

    246396-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.