1

Let say JSON string is given, and I want to validate using C#. We all know that JSON string has the following format

string jsonStr = {"Id":123,"Value":"asdf","Time":"adf","isGood":false}];

I want to take care of Number, String, Boolean, Null types for now. I can see that the pattern of JSON is

{ + " + String + " + : + (Number)|(Boolean)|(" + String + ")|(Null) + , + ... + } + ]
// ... means one or more

I am really new to Regular Expression, so I have no idea... Could anyone kindly help me out?

EDIT
Sorry, I am not using JSON.NET and I don't want to use it. I found that using Regex is the only way to validate my JSON string. If there is any suggestion, I will go for it. Thank you

EDIT2
My question is "How to validate JSON using Regex", and not "Should I validate JSON using Regex". You guys probably understand that company has own policy "not to use 3rd-party resource". What should I do guys? I am just NOT ALLOWED to use it.

11
  • Generally you need a JSON parser to validate JSON. Regex is not appropriate tool for the job. Commented Oct 24, 2013 at 20:21
  • 1
    Exactly. You should be using JSON.Net. Commented Oct 24, 2013 at 20:23
  • 1
    sorry, I don't want to use 3rd party resource. Commented Oct 24, 2013 at 20:25
  • There's a lot of things to consider if you opt to write your validator: .net DateTimes for once... Commented Oct 24, 2013 at 20:29
  • 4
    How about .net's native DataContractJsonSerializer? msdn.microsoft.com/en-us/library/bb412179.aspx Commented Oct 24, 2013 at 20:41

5 Answers 5

4

I'm going to put this at the top of my JSON-knowledge-lacking attempt so everyone sees it:

Regex to validate JSON

Basically, to everyone who's losing their minds over this, modern regex implementations have gone farther than formal cs regular expressions, and as a result are no longer bound to representing only regular languages, because of things like backreferences and recursion. Ergo, we can now match things with regex that aren't regular languages, which, I'll give you, is rather unintuitive.



I'll leave my attempt here for posterity anyway.

This pattern:

 {("\w+":(\d+|"\w+"|true|false|null))+}\]

should match what you're asking for if I understand you correctly, but from the storm of angry posts, it seems that you probably shouldn't use regex.

Sign up to request clarification or add additional context in comments.

12 Comments

What about nested objects and arrays? And arrays with nested objects, and nested arrays? JSON should NOT be validated with regex.
@xbonez I understand that there are a lot of types such as nested objects or arrays. But, for now (for simplification) I only care the simple types that I wrote in the question.
He didn't ask if he should, just how, and I've never worked with JSON so I wouldn't know, just trying to see if I still remember regex.
Also, your regex is malformed. The ending ] is mismatched.
From the looks of his post, that's supposed to be at the end. If he made a typo, I inherited it.
|
4

Just an idea, why not deserialyze data first and then validate fields:

var serializer = new JavaScriptSerializer();
dynamic obj = serializer.Deserialize(json, typeof(object));

Then you can validate: obj.Id, obj.Value, obj.isGood

1 Comment

Validating a JSON before deserializing it comes very handy. Personally, I use this approach in my app to communicate with other 3rd party APIs and it reduces me of conditional checking as sometimes instead of JSON plain text is returned in case of error which is serious pain.
0

If you're not forced to use RegEx and you just need a syntax check of JSON data for debugging purpose, try the online tool

http://jsonlint.com/

which does the job fine (caution: do not paste sensitive data!).

Example: If you paste the data

{
    "Id": 123,
    "Value": "asdf",
    "Time": "adf",
    "isGood": false
}]

You're getting the result:

Parse error on line 6:

... "isGood": false}]

------------------------^ Expecting 'EOF'

If you do have to validate sensitive data, you can look at the source code, which is available at GitHub:

https://github.com/arc90/jsonlintdotcom.

There is also a pure JS version available: https://github.com/zaach/jsonlint.


If you intend to do a schema-based validation, e.g. to determine if the JSON data consists of the right data types, look here:

https://stackoverflow.com/questions/19544183/validate-json-against-json-schema-c-sharp


N.B. RegEx can only parse regular grammars; for anything context-free and higher you need a stack (i.e. a real parser). This is why the solutions shown here are not using RegEx - regular expressions are typically used to parse number formats, the format of domain names, email addresses etc. For this purpose, they are doing the job good. For real parsing tasks as mentioned above better use a real parser.

Comments

0

I tried to validate using what is commented in the accepted answer without success using this regex pattern:

 {("\w+":(\d+|"\w+"|true|false|null))+}\]

I tried with this pattern {[^}]+} and now I have a good validation:

            WebClient client = new WebClient();
            string myJSON = client.DownloadString(jsonFile);
         
            //var regex = @"{(\w+:(\d+|\w + |true|false|null))+}\]";
            var regex = @"{[^}]+}";
            var match = Regex.Match(myJSON, regex, RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                Debug.WriteLine("* JSON NOT VALID.");                   
            }
            else
            {
                Debug.WriteLine("* JSON VALID.");                   
            }

Comments

0

I think this is already solved with above solutions, but if you are looking for a very clean approach to this where only a single line of code does your job then you might wanna check this open source library I have created:

Its open, scalable and provides enough utilities for most of the requirements. Define a class as you normally would, rest of things are taken care by the library. It will recursively search entire JSON while parallely comparing it to your predefined class.

https://github.com/anirudha-stack/JsonScrutinize

https://www.nuget.org/packages/JsonScrutinize

Nested json is supported, you can get Mismatched type keys, null keys and missing keys from this.

Quick Start Guide: Install via NuGet

Install-Package JsonScrutinize

Define your C# class with validation attributes:

public class User
{
    [Required]
    public string Name { get; set; }
    
    [RegularExpression(@"^[A-Za-z]+$")]
    public string Role { get; set; }
}

Validate your JSON:

string json = "{\"Name\": \"John Doe\", \"Role\": \"Admin123\"}";
JsonScrutinize scrutinizer = new JsonScrutinize();
var result = await scrutinizer.KeyTypeScrutinize(json, typeof(User));

Expected Output:

{
    "StatusCode": "S201",
    "Description": "Found 1 mismatched key in the provided JSON",
    "MismatchedKeys": [
        "Role"
    ]
}

You can also check the source code of it in the github release.

Happy coding :)

2 Comments

Please don’t spam the site with copy/pasted "answers".
OP requires what the library offers, I have a standard readme created for showcasing the usage of library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.