LoginSignup
3
3

More than 3 years have passed since last update.

JSONの形式の仕様ざっくりまとめとサンプル集

Last updated at Posted at 2020-01-14

JSONの仕様のざっくりまとめ

まず、以下のドキュメントで仕様をざっくり確認する。

RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format https://tools.ietf.org/html/rfc8259

  • JSONは4つのプリミティブ型(文字列、数値、Bool、null)と2つの構造のタイプ(オブジェクト、配列)から構成される
  • "値"とは右記のものである:オブジェクト、配列、数値、文字列、Bool、null
  • オブジェクトは以下のフォーマットのものである
    • {"文字列": 値}
    • 左部の名前(キー)は重複できない。重複したときの動きは実装依存(かな、下記の通り)。
  • 配列は右記のフォーマット:[値, 値…]

When the names within an object are not unique, the behavior of software that receives such an object is unpredictable.

サンプル

上記のとおり、構造のタイプとして大きく「配列」と「オブジェクト」がある。

配列

配列は0個以上の"値"をカンマで区切ったもの。値にはオブジェクトも含まれる。具体的には、オブジェクト、配列、数値、文字列、Bool、null

サンプル

[0]
[null]
[true]
[true, 0, null]
[0,0,0]
[
    {
        "name": "value"
    }
]
[
    {
        "name": "value"
    },
    0,
    true
]

オブジェクト

中括弧で文字列と値をコロンで結んだもの。
値にはオブジェクトも含まれるので入れ子も可能。

{
    "name1": 0
}
{
    "name1": true,
    "name2": false,
    "name3": null
}
{
    "name4": {
        "name5": 0
    }
}
{
    "hoge": [
        1,
        2,
        3
    ]
}

その他

構文チェックは以下のサイトで確認:

JSON Pretty Linter - JSONの整形と構文チェック https://lab.syncer.jp/Tool/JSON-Viewer/

3
3
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
3
3