简体   繁体   English

如何在Python3中验证json结构

[英]How to validate a json structure in Python3

I am using Python3.5 and Django for a web api.When I refer to input, I refer to a HTTP request parameters. 我使用Python3.5和Django作为Web API,当我提到输入时,我指的是HTTP请求参数。 I have a parameter where I am expecting a JSON data which I need to validate before processing further. 我有一个参数,我期望在进一步处理之前需要验证的JSON数据。

I have a base json structure that the input has to be in. Example, 我有一个必须输入的基本json结构。例如,

{
  "error": "bool",
  "data": [
      {
        "name": "string",
        "age": "number"
      },
      {
        "name": "string",
        "age": "number"
      },
      ...
    ]
}

The above JSON represents the structure that I want my input to be in. The keys are predefined, and the value represents the datatype of that key that I am expecting. 上面的JSON表示我希望输入的结构。键是预定义的,值表示我期望的键的数据类型。 I came across a Python library( jsonschema ) that does this validation, but I can't find any documentation where it works with dynamic data. 我遇到了执行此验证的Python库( jsonschema ),但找不到任何可用于动态数据的文档。 ie the objects inside the JSON array 'data' can be of any number, of course this is the most simple scenario I came up with for explaining the basic requirement. 也就是说,JSON数组“数据”中的对象可以是任意数量,当然,这是我为解释基本要求而提出的最简单的方案。 In cases like these, how can I validate my json ? 在这种情况下,如何验证json

The solution here didn't help because it's just checking if the json is proper or not based on the Django model. 这里的解决方案没有帮助,因为它只是根据Django模型检查json是否正确。 My json has no relation with Django model. 我的json与Django模型无关。 Its a simple json structure. 它是一个简单的json结构。 It still doesn't tell me how to validate dynamic object 它仍然没有告诉我如何验证动态对象

JSON Schema is a specification for validating JSON; JSON模式是用于验证JSON的规范; jsonschema is just a Python library that implements it. jsonschema只是实现它的Python库。 It certainly does allow you to specify that a key can contain any number of elements. 当然,它确实允许您指定键可以包含任意数量的元素。

An example of a JSON Schema that validates your code might be: 验证代码的JSON模式示例如下:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "error",
    "data"
  ],
  "properties": {
    "error": {
      "type": "boolean"
    },
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "name": {
            "type": "string"
          },
          "age": {
            "type": "integer"
          }
        }
      }
    }
  }
}

See https://spacetelescope.github.io/understanding-json-schema/ for a good overview 请参阅https://spacetelescope.github.io/understanding-json-schema/以获取良好的概述

Take a look into the documentation of Python's JSON API . 查看Python的JSON API文档。 I believe json.tool is what you're looking for, however there are a couple of other ways to validate JSON using that API. 我相信json.tool是您要找的东西,但是还有其他几种使用该API验证JSON的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM