Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a JSON String JQuery [duplicate]

I tried that:

var c = $.parseJSON(something here)

and I control that:

c === undefined

This works however it throws error while trying to parse an invalid JSON string. I don't want it throw that error.

Any advices?

like image 999
kamaci Avatar asked Dec 06 '25 06:12

kamaci


1 Answers

It's generally considered bad practice to suppress/ignore errors, instead why not use a try-catch block to capture the exception and do something with it:

try {
  var c = $.parseJSON(something here);
}
catch (err) {
  // Do something about the exception here
}

If you really don't need to do anything about the exception at least put a comment to that effect in your try-catch block, it'll make your code more readable when you come back to it later.

like image 183
Clive Avatar answered Dec 07 '25 20:12

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!