JSONでよくある間違い
- 知る
- 直す
JSONは、書き方が厳密で、ちょっとした間違いで読めなくなります。JSONを整える・チェックする で「JSONの形が正しくありません」と出たときに、確認したい、よくある間違いをまとめます。
手順
- 「整える前」に、JSONを貼り付けます。
- 「整える」を押します。
- 誤りがあれば、「JSONの形が正しくありません」と表示されます。
- 下のよくある間違いを、順に確認して直します。
よくある間違い
- 最後の項目の後ろに、余分なカンマ:{"a": 1, "b": 2,} は誤り。最後のカンマを消す。
- シングルクォーテーションを使っている:JSONは、ダブルクォーテーション(")のみ。
- 項目名に引用符がない:{name: "山田"} は誤り。{"name": "山田"} とする。
- かっこの閉じ忘れ:{ と }、[ と ] の数を合わせる。
- カンマの抜け:項目と項目のあいだに、カンマが必要。
- コメントを書いている:JSONには、コメントを書けない。
- 全角の記号:全角のかっこ、引用符、コロン、カンマは使えない。
- 値の書き方の誤り:true と false と null は小文字。True や NULL は誤り。
- 数値の書き方:先頭のゼロ(007)や、末尾の小数点は誤り。
- 文字列中の改行:文字列の中に、そのまま改行を入れられない。「\n」と書く。
- 文字列中の引用符:「\"」と、エスケープが必要。
正しい形の例
波かっこの中に、項目名と値を、コロンでつなぎ、項目のあいだをカンマで区切る。項目名と文字列は、ダブルクォーテーションで囲む。
直すコツ
- 最後に編集した箇所を、まず見る
- 長い場合は、半分に分けて、どちらに誤りがあるか絞る
- 見やすく整えられるまで、少しずつ直す
全角の記号の見つけ方
見た目は似ていても、全角のかっこや引用符は、半角と幅が違います。テキストエディタで、全角の記号を検索して、探します。
向いている対処
手書きや、メモ帳で編集したJSONに多い誤りです。編集後は、必ずこのツールで、チェックしましょう。
例
設定ファイルを編集したら、読み込めなくなった。調べると、項目名の引用符が、全角になっていた。半角に直して、解決した。
つまずきやすい点
- 直したのに、まだ誤り:他にも誤りがある。繰り返し確認する。
- エラーの場所が分からない:短くして、試す。
よくある質問
JavaScriptのオブジェクトは、そのまま貼れますか?
項目名に引用符がなければ、誤りになります。JSONの書き方に直します。
まとめ
余分なカンマ、引用符、かっこ、全角に注意。直したら、もう一度チェックしましょう。
JSONを整える・チェックするを使う(無料)あわせて使えるツール
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルとBase64を相互変換する:ファイルとBase64の文字を相互に変換します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
同じツールの、ほかの解説
Common JSON Mistakes
- Learn
- Fix
JSON syntax is strict, and a small mistake makes it unreadable. Here are the common mistakes to check when Format and Validate JSON shows "The JSON is malformed".
Steps
- Paste the JSON into "Before".
- Press "Format".
- If there is an error, "The JSON is malformed" is shown.
- Check the common mistakes below in order and fix them.
Common mistakes
- An extra comma after the last item: {"a": 1, "b": 2,} is wrong. Delete the last comma.
- Using single quotes: JSON uses only double quotes (").
- No quotes on item names: {name: "Yamada"} is wrong. Write {"name": "Yamada"}.
- Unclosed brackets: match the numbers of { and }, [ and ].
- Missing commas: a comma is needed between items.
- Writing comments: JSON can't have comments.
- Full-width symbols: full-width brackets, quotes, colons and commas can't be used.
- Wrong value notation: true, false and null are lowercase. True and NULL are wrong.
- Number notation: leading zeros (007) and trailing decimal points are wrong.
- Line breaks in strings: a raw line break can't go inside a string. Write "\n".
- Quotes in strings: they need escaping as \".
Example of the correct form
Inside braces, join item names and values with colons and separate items with commas. Enclose item names and strings in double quotes.
Tips for fixing
- Look first at the place you edited last
- If long, split it in half and narrow down which half has the error
- Fix a bit at a time until it can be formatted
Finding full-width symbols
Though they look similar, full-width brackets and quotes differ in width from half-width. Search for full-width symbols in a text editor.
A useful approach
These are common mistakes in JSON edited by hand or in Notepad. After editing, always check with this tool.
Example
After editing a configuration file it couldn't be loaded. Investigation showed the quotes on the item names were full-width. Changed to half-width and solved it.
Common stumbling blocks
- Fixed but still an error: there are other errors. Check repeatedly.
- Can't tell where the error is: shorten and try.
Frequently asked questions
Can a JavaScript object be pasted as it is?
If item names lack quotes, it's an error. Rewrite it in JSON syntax.
Summary
Watch for extra commas, quotes, brackets and full-width characters. After fixing, check again.
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.