Solved

How do I validate JSON using C#/Xunit?

Posted on 2009-06-29
2
2,433 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
[X]
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
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

624 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