1

Possible Duplicate:
How to check if a string is a valid JSON string in JavaScript without using Try/Catch

The question was already asked here : How to check if a string is a valid JSON string in JavaScript without using Try/Catch. No valid answer was given, even the validated one wasn't answering the question.

So it seems the only way to not doing this using try/catches is via regexps; the regexp given in one of the answers was only validating against eval, so a simple string like "2001-05-06" would pass the regexp, even though it's not JSON.

So has anyone a good regexp to validate against a well formed JSON string ?

8
  • 4
    Using a regex instead of a try/catch is replacing the correct solution with a non working hack. Commented Nov 13, 2012 at 10:39
  • 2
    Why don't you want to use try .. catch? Seems quite legit to use, because it throws an exception when failed. Commented Nov 13, 2012 at 10:43
  • 1
    Assuming there is some JS library that painstakingly implements a fully correct JSON parser without using try/catch internally -- I 'm curious to know how you would justify adding tons of code and accepting the performance hit just so that you don't write a 3-line solution that does use try/catch. And that's without considering that more code == more bugs at all. Commented Nov 13, 2012 at 10:43
  • 2
    If it was already asked, you should promote that question under current rules, not repeat it. Commented Nov 13, 2012 at 10:58
  • It's a matter of preference. I prefer code that looks like this : if(condition){do things}else{explain why you couldn't do things to the user} instead of try{to do things}catch(an exception){if it failed}. I can't remember last time I used a try/catch ? except maybe for python code that dealt with databases and would have to catch an error if something went wrong with the database. It seems perfectly legitimate to me to use a condition here rather than an a try clause. Also, why would it be hackish to implement a json parser ? isn't crocksford's code exactly that ? Commented Nov 13, 2012 at 16:48

2 Answers 2

2

Using a regex instead of a try/catch is replacing a correct solution with a non working hack.

The link you give suggests to change the JSON parsing code that you can modify to not throw an exception. I would suggest replacing in json_parse.js the following code

    error = function (m) {
    // Call error when something is wrong.
        throw {
            name:    'SyntaxError',
            message: m,
            at:      at,
            text:    text
        };
    },

by the call of a callback you would provide.

But to be frank, my most reasonable suggestion would be to use try/catch. That's the best tool you have here. Note that my JSON.parse modification "suggestion" supposes to manage the break from all loops/recursions, which is precisely what an exceptions does.

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

Comments

0

from link try this

var jsonData = '{"1":"test"}';

if (/^[\],:{}\s]*$/.test(jsonData.replace(/\\["\\\/bfnrtu]/g, '@'). replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {    
    alert('ok');
}else{    
    alert('no');
}

2 Comments

My first test : '{"1":"test":"a"}'; ==> appears as valid JSON....
As both the original poster and myself commented, a simple string like "2012-12-07" would pass that test, yet it's not valid json.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.