0

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 ?

Community
  • 1
  • 1
ychaouche
  • 4,242
  • 2
  • 40
  • 45
  • 4
    Using a regex instead of a try/catch is replacing the correct solution with a non working hack. – Denys Séguret Nov 13 '12 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. – dan-lee Nov 13 '12 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. – Jon Nov 13 '12 at 10:43
  • 2
    If it was already asked, you should promote that question under current rules, not repeat it. – Oleg V. Volkov Nov 13 '12 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 ? – ychaouche Nov 13 '12 at 16:48
  • @OlegV.Volkov what's the recommanded way of getting an answer for my question in the other thread considering an answer had already been accetped there ? – ychaouche Nov 13 '12 at 17:16
  • While I can completely agree with the argument of using the try .. catch block, this is a real problem in SO of people closing perfectly valid questions because they think it is a duplicate of another. Obviously ychaouche was not satisfied with the answer on the other post and would like an alternative. Now no one can post anything to address his question because a few people deemed it as already answered. I can think of several good approaches that wouldn't bloat the code much more than try/catch. Open discourse and problem-solving is being blocked based on individual bias. Not great... – jtrick Dec 03 '12 at 12:53
  • Well, the question is really a duplicate as everybody mentioned but the answers given to the original question are not really answering it, they're rather saying that the if/else approach is wrong and the try/catch one is right. That's fine, except it doesn't answer the question :). So suppose one isn't convinced that if/else approach is wrong, like myself, how can one get an answer ? there's probably nobody who thought about this situation and SO, as a tool, do not know how to handle this. That's how I see it. – ychaouche Dec 03 '12 at 16:58

2 Answers2

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.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
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');
}
Community
  • 1
  • 1
Kotzilla
  • 1,083
  • 15
  • 23