LoginSignup
7
2

More than 3 years have passed since last update.

JavaScriptでJSONが不正かどうか判定する関数

Last updated at Posted at 2019-05-14

JSONを文字列からパースするための関数としてJSON.parse()が存在するが、不正な文字列を与えた時にはSystaxErrorをthrowすることを利用して下記のような関数を実装できる。

isValidJson(value) {
  try {
    JSON.parse(value)
  } catch (e) {
    return false
  }
  return true
}
7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2