JSONの書き方の間違いを見つける方法
- 貼る
- 確かめる
- 直す
JSONを書いたら、プログラムが読めずに、エラーになった。どこが間違っているのか分からない。JSONを整える・チェックする は、貼り付けたJSONが正しいかを、チェックします。
手順
- 「整える前」に、JSONを貼り付けます。
- 「整える」を押します。
- 正しい場合は、「正しいJSONです」と表示され、「整えた後」に整った結果が出ます。
- 誤りがある場合は、「JSONの形が正しくありません」と表示されます。
チェックの流れ
- 貼り付けたJSONが、正しいか確認する。
- 誤りがあれば、直す。
- もう一度「整える」で確認する。
- 「正しいJSONです」と出れば、書き方は合っている。
誤りを探すコツ
- 短くして試す:JSONの一部だけを貼り付けて、どこから誤りが出るか探す。
- かっこの対応を見る:{ と }、[ と ] の数が合っているか。
- カンマを見る:項目と項目のあいだにあるか、最後の項目の後ろに余分にないか。
- 引用符を見る:項目名と文字列が、ダブルクォーテーション(")で囲まれているか。
正しいと判定される例
項目名がダブルクォーテーションで囲まれ、値が文字列・数値・真偽値・null・配列・オブジェクトのいずれか。
正しいJSONでも注意すること
「正しいJSONです」と出ても、プログラムが期待する項目や形式と合っているとは限りません。項目名の綴りや、値の型は、別途確認します。
向いている場面
- 設定ファイルを編集したあとの確認
- 手書きのJSONの、チェック
- APIに送る前の、確認
- コピーしたJSONが、欠けていないかの確認
例
設定ファイルを手で編集したら、アプリが起動しなくなった。貼り付けて確認したところ、「JSONの形が正しくありません」。最後の項目の後ろの余分なカンマを消したら、直った。
つまずきやすい点
- エラーの場所が分からない:短くして試す。
- 全角の記号が入っている:全角のかっこや引用符は、使えない。
よくある質問
JSON5やコメント付きのJSONはチェックできますか?
通常のJSONの規則で判定します。コメントは、書けません。
まとめ
貼って「整える」を押すだけで、書き方をチェック。誤りは、よくある間違いから探しましょう。
JSONを整える・チェックするを使う(無料)あわせて使えるツール
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルとBase64を相互変換する:ファイルとBase64の文字を相互に変換します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
同じツールの、ほかの解説
How to Find Mistakes in JSON
- Paste
- Check
- Fix
You wrote JSON, and the program couldn't read it and gave an error. You can't tell what's wrong. Format and Validate JSON checks whether the pasted JSON is correct.
Steps
- Paste the JSON into "Before".
- Press "Format".
- If correct, "Valid JSON" is shown and the tidy result appears under "After".
- If there is an error, "The JSON is malformed" is shown.
Flow of checking
- Check whether the pasted JSON is correct.
- If there is an error, fix it.
- Check again with "Format".
- If "Valid JSON" appears, the syntax is right.
Tips for finding errors
- Shorten and try: paste just part of the JSON and look for where the error starts.
- Check bracket pairing: do the numbers of { and }, [ and ] match?
- Check commas: present between items, and no extra after the last item?
- Check quotes: are item names and strings enclosed in double quotes (")?
Examples judged correct
Item names enclosed in double quotes, and values that are a string, number, boolean, null, array or object.
Even correct JSON needs care
Even if "Valid JSON" appears, it doesn't necessarily match the items and form the program expects. Check the spelling of item names and value types separately.
Good for
- Checking after editing a configuration file
- Checking hand-written JSON
- Checking before sending to an API
- Checking that copied JSON isn't truncated
Example
After hand-editing a configuration file, the app wouldn't start. Pasting it in showed "The JSON is malformed". Deleting an extra comma after the last item fixed it.
Common stumbling blocks
- Can't tell where the error is: shorten and try.
- Full-width symbols included: full-width brackets and quotes can't be used.
Frequently asked questions
Can JSON5 or JSON with comments be checked?
It is judged by the rules of ordinary JSON. Comments can't be written.
Summary
Just paste and press "Format" to check the syntax. Look for errors starting with the common mistakes.
Use "Format and Validate JSON" (free)Tools that go well with this
- Convert CSV and JSON: Convert between CSV and JSON instantly.
- Convert Files and Base64: Convert between files and Base64 text.
- Remove Duplicate Lines and Sort a List: Remove duplicate lines and sort them.