JSON validation using regex


Fiskal Research

After mucking around for ages with finding problems in my JSON, I decided to make a JSON validator.

I spent the morning looking at php ReGex, I think that I have my head around the basics of it now.

The first of the tutorials is located here on youtube

The website that he uses to check his RegEx is here

So after mucking around for a bit, this is what I wrote.

function htmlCheck($json){
 $exp = preg_split('/((\[\{)|(,\{)|((\s)+,))((\s|)+"[a-zA-Z0-0]+":"[a-zA-Z0-9_:;\s]+"(\s|)+(,|\})(\s|)(\]|))+/', $json);
 foreach ($exp as $value){
 if($value != null)
 return print_r($exp);
 }
 return '{"html":'.json_encode(json_decode($json)).'}';
}

Still needs more work, but presently it takes a json array[{“”:””}] etc and returns anything that isn’t valid as part of the split. I’m not sure if this is the best method, but it returns the mistake and surrounding code

For example, if I have

$json ='[{
 "jParent":"dockLeft",
 "jElem":"form",
 "jId":"signup"
 }(more objects here)]

And remove the first quote from…

View original post 70 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s