LoginSignup
1
2

More than 5 years have passed since last update.

JSON の構文の正当性をチェックする

Last updated at Posted at 2012-12-26

パッケージマネージャで yajl をインストールすると json_verify コマンドがついてくる。このコマンドは標準入力に渡された JSON テキストの構文が正しいかどうかチェックする。

> echo "[1, 2, 3]" | json_verify
JSON is valid

> echo "[1, 2 3]" | json_verify
parse error: after array element, I expect ',' or ']'
                                        [1, 2 3] 
                     (right here) ------^
JSON is invalid

ただし json_vefify は JSON テキストのトップレベルに object または array 以外の値があっても valid と判定する点に注意されたい( JSON の仕様 (RFC4627)は、トップレベルの値が object または array でなくてはならないと定めている)。2015年10月追記: 新しい JSON の仕様(RFC7159)はトップレベルに任意の値を許すようになった。

> echo '"string"' | json_verify
JSON is valid

> echo 42 | json_verify
JSON is valid
1
2
2

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
1
2