LoginSignup
1
1

More than 1 year has passed since last update.

jsonschemaで「オブジェクトの配列」を定義する (pythonで実装)

Posted at

概要

  • こういうjsonのデータの jsonschema を定義したい
employees = [
    {
        "id": 12,
        "name": "Tom",
    },
    {
        "id": 24,
        "name": "Bob",
        "age": 28,
    },
]

環境

  • Ubuntu 20.04.2 LTS
  • Python 3.8.10 (venv)
  • jsonschema 3.2.0

パッケージのインストール

pip install jsonschema 

シンプルな方法

  • "type" 要素を "array" に設定
  • "items" 要素を定義して、
  • "items" 要素の値に 「"type" が "object"の値」を定義
jsonschema_array.py
import json
from jsonschema import validate, ValidationError


def main():
    """
    https://json-schema.org/learn/miscellaneous-examples.html
    """
    employee_jsonschema = {
        "$id": "",
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "description": "Array of things without ref/defs keyword.",
        "type": "array",
        "items": {
            "type": "object",
            "required": ["id", "name"],
            "properties": {
                "id": {
                    "type": "number"
                },
                "name": {
                    "type": "string"
                },
                "age": {
                    "type": "number"
                }
            },
            "additionalProperties": False
        }
    }

    employees = [
        {
            "id": 12,
            "name": "Tom",
        },
        {
            "id": 24,
            "name": "Bob",
            "age": 28,
        },
    ]

    result = "OK"

    try:
        validate(employees, employee_jsonschema)
    except ValidationError as e:
        result = e.message

    print(result)


if __name__ == '__main__':
    main()


# OK

参照を使う例

  • ネストが深くなって見づらくなるのを回避
  • ref と defs というキーワードを利用
  • ref / defsを紐づけるための id (この例では、employee)を設定
jsonschema_array_ref.py
import json
from jsonschema import validate, ValidationError


def main():
    """
    https://json-schema.org/learn/miscellaneous-examples.html
    """
    employee_jsonschema = {
        "$id": "",
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "description": "Array of things using by ref/defs keyword.",
        "type": "array",
        "items": {"$ref": "#/$defs/employee"},
        "$defs": {
            "employee": {
                "type": "object",
                "required": ["id", "name"],
                "properties": {
                    "id": {
                        "type": "number"
                    },
                    "name": {
                        "type": "string"
                    },
                    "age": {
                        "type": "number"
                    }
                },
                "additionalProperties": False
            }
        }
    }

    employees = [
        {
            "id": 12,
            "name": "Tom",
        },
        {
            "id": 24,
            "name": "Bob",
            "age": 28
        },
    ]

    result = "OK"

    try:
        validate(employees, employee_jsonschema)
    except ValidationError as e:
        result = e.message

    print(result)


if __name__ == '__main__':
    main()


# OK

参考

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