Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I validate JSON using C#/Xunit?

Posted on 2009-06-29
2
2,387 Views
Last Modified: 2013-11-08
After spending two days of research on the simple validation of XML in Xunit (since noone was willing to help with my previous XML validating question), I've finally by myself written a very simple XML syntax validation code in C#. (code SIMPLIFIED EVEN FURTHER below)

Now I am faced with the same problem but this time is validation of JSON.

Please consider the code below and help me with write a simple and straight-forward solution just like the XML validation.

THANKS.


PS, I apologize if I sound a tad rude in my question, but I am losing patient with this PAID website.
using System;
using Xunit;
using System.Xml;
 
public class XMLisValid 
{
    [Fact]
    public void testXMLValid(string source) 
    {
        XmlDocument doc = new XmlDocument();
 
       Assert.DoesNotThrow(
                delegate
                {
                    doc.Load(source);
                });   
    }
}

Open in new window

0
Comment
Question by:dy211
2 Comments
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 100 total points
ID: 24744057
To validate the JSON string you need to parse the JSON format and retrieve the elements.  There is a JSON parser here:
  http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
and you can parse it like this article does:
  http://www.jamiedigi.com/2009/06/parsing-twitter-json-in-c-sharp/

So your test code would look something like this:


    [Fact]
    public void testJSONValid(string source) 
    {
       HashTable jsonHash = (Hashtable)Procurios.Public.JSON.JsonDecode(jsonCode);
 
       ArrayList jsonResults = (ArrayList)jsonHash["results"];
       foreach (object objResult in jsonResults) {
          //Add asserts here asserting that jsonResults contains correct values(i.e.  jsonResult["ExpectedVariableName"]  should contain ???)
       }
    }

Open in new window

0
 

Accepted Solution

by:
dy211 earned 0 total points
ID: 24817586
solved this question myself by running a script to run http://www.raboof.com/projects/jsonchecker/
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question