【问题标题】:Compiling json schema using fastjsonschema gives TypeError: string indices must be integers使用 fastjsonschema 编译 json 模式给出 TypeError: string indices must be integers
【发布时间】:2020-01-20 13:40:48
【问题描述】:

我正在使用 fastjsonschema 来根据其架构验证 json 记录。 compile 方法适用于简单模式,但是在使用引用 fastjsonschema 编译 json 模式时会出错。 TypeError:字符串索引必须是整数。下面是代码 sn-p、json 架构和错误。

编译架构的代码#####
import fastjsonschema

schema_file = open(schema.json)
schema_str = schema_file.read()
validatation = fastjsonschema.compile(schema_str)
schema.json 的内容
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "organization",
    "description": "JSON schema",
    "type": "object",
    "properties": {
        "transactionDetail": {
            "id": "https://example.com/transactionDetail",
            "type": "object",
            "properties": {
                "transactionID": {
                    "description": "A number assigned by the calling application to uniquely identify this request.",
                    "type": "string"
                },
                "transactionTimestamp": {
                    "description": "The date and time when this request was submitted.",
                    "type": "string"
                }
            },
            "required": [
                "transactionID"
            ],
            "additionalProperties": false
        },
        "organization": {
            "$ref": "#/definitions/organization"
        }
    },
    "additionalProperties": false,
    "definitions": {
        "organization": {
            "type": "object",
            "properties": {
                "identifier": {
                    "description": "identification number.",
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 12
                },
                "countryCode": {
                    "description": "The two-letter country code.",
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 2
                },
                "timestamp": {
                    "description": "The date and time that the record was created.",
                    "type": "string"
                },

                "required": [
                    "identifier",
                    "countryCode"
                ],
                "additionalProperties": false
            }
        }
    }
}

Expected Result: Nothing

Actual Result:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/__init__.py", line 153, in compile
  resolver, code_generator = _factory(definition, handlers)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/__init__.py", line 193, in _factory
  resolver = RefResolver.from_schema(definition, handlers=handlers)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 89, in from_schema
  **kwargs
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 78, in __init__
  self.walk(schema)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 148, in walk
  elif '$ref' in node and isinstance(node['$ref'], str):
      TypeError: string indices must be integers

【问题讨论】:

    标签: python json python-3.x schema fastjson


    【解决方案1】:

    我也遇到过这个问题,看来您需要使用 dict(而不是 str)来提供 fastjsonschema 编译和验证方法。

    一种简单的方法是在 Python 中使用 json 模块:

    with open(myschema, 'r') as file:
      schemadict = json.loads(file.read())
      validate = fastjsonschema.compile(schemadict)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2017-10-31
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2020-10-18
      • 2020-09-04
      • 2022-09-23
      相关资源
      最近更新 更多