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.

Adrian
  • 836
  • 7
  • 20
  • 44
  • Generally you need a JSON parser to validate JSON. Regex is not appropriate tool for the job. – Andrew Savinykh Oct 24 '13 at 20:21
  • 1
    Exactly. You should be using JSON.Net. – SLaks Oct 24 '13 at 20:23
  • 1
    sorry, I don't want to use 3rd party resource. – Adrian Oct 24 '13 at 20:25
  • There's a lot of things to consider if you opt to write your validator: .net DateTimes for once... – Dimitri Oct 24 '13 at 20:29
  • If you don't want to use 3rd party sources, you need to build your own JSON parser. Just not with Regex. Start but taking the JSON grammar and putting it through a lexer to generate proper code that can parse JSON. This is a lot of fun! – Andrew Savinykh Oct 24 '13 at 20:30
  • http://msdn.microsoft.com/en-us/library/system.json.jsonobject – SLaks Oct 24 '13 at 20:31
  • @David Armo If you read my question, I was asking for help how to create REGEX. Am I wrong? – Adrian Oct 24 '13 at 20:36
  • @David Armo Yes, that's good enough "Regex won't help your validation work". That's it. I really appreciate for your suggestion, but I am just NOT ALLOWED to use JSON.Net. That's why I am posing my question in here. If I am able to use JSON.Net, I don't even have to ask this question – Adrian Oct 24 '13 at 20:41
  • 4
    How about .net's native DataContractJsonSerializer? http://msdn.microsoft.com/en-us/library/bb412179.aspx – Dimitri Oct 24 '13 at 20:41
  • There's a world of difference between "don't want to use 3rd party resource" and "cannot, due to company policy, use a 3rd party resource". If you'd made that clear in the first place then you probably wouldn't have got the down-votes and negative comments. – ChrisF Oct 31 '13 at 20:24
  • While I don't agree that RegEx is the only way to validate JSON, I don't understand why there are so many downvoters. I have this issue as well (needing a JSON validator), and I found [this](http://stackoverflow.com/a/33280066/1016343) solution. Btw. I have upvoted this question. – Matt Oct 22 '15 at 11:37

4 Answers4

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.

Community
  • 1
  • 1
NathanTempelman
  • 1,301
  • 9
  • 34
  • What about nested objects and arrays? And arrays with nested objects, and nested arrays? JSON should NOT be validated with regex. – Ayush Oct 24 '13 at 20:26
  • 1
    @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. – Adrian Oct 24 '13 at 20:27
  • 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. – NathanTempelman Oct 24 '13 at 20:28
  • Also, your regex is malformed. The ending `]` is mismatched. – Ayush Oct 24 '13 at 20:31
  • From the looks of his post, that's supposed to be at the end. If he made a typo, I inherited it. – NathanTempelman Oct 24 '13 at 20:33
  • That's some pretty heavy downvoting, did I in fact forget how to regular expression? – NathanTempelman Oct 24 '13 at 20:38
  • @NathanTempelman I think the problem is just that since JSON is not a regular language, it's impossible to parse it using a regular expression. See: http://cstheory.stackexchange.com/a/4017 – asawyer Oct 24 '13 at 20:50
  • I don't know anything about JSON, so I didn't know that it wasn't just in the pattern he specified. That said, regex doesn't only fit regular expressions. Utilities have been added to it. Take a look: http://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non-regular_languages – NathanTempelman Oct 24 '13 at 20:53
  • @NathanTempelman Upvote. I appreciate your help. I was just trying to build some Regex from experts, but people are mad at me because I don't use JSON.Net. – Adrian Oct 24 '13 at 20:54
  • Yeah I see that. Take a look at the question I found, that seems to actually do it properly and it's by someone who knows both JSON and regex well. – NathanTempelman Oct 24 '13 at 20:58
  • More simple one: `{[^}]+}` – FindOutIslamNow Feb 26 '19 at 11:21
  • Just try to parse it and handle exceptions ... isnt that obvious? – UbuntuHusker Sep 18 '19 at 14:55
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

nikolai.serdiuk
  • 762
  • 8
  • 11
  • 1
    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. – Nadim Hossain Sonet Aug 29 '17 at 11:41
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.

Community
  • 1
  • 1
Matt
  • 25,467
  • 18
  • 120
  • 187
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.");                   
            }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268