首页 > 解决方案 > JSON 模式生成器 Python

问题描述

我正在使用此资源生成架构https://github.com/wolverdude/GenSON/

我有以下 JSON 文件

{
 'name':'Sam',
},
{
 'name':'Jack',
}

很快 ...

我想知道如何迭代一个大的 JSON 文件。我想解析每个 JSON 文件并将其传递给 GENSON 以生成模式

{
  "$schema": "http://json-schema.org/schema#",
  "type": "object",
  "properties": {
     "name": {
       "type": [
        "string"
      ]
   }
},
  "required": [
    "name"
  ]
}

标签: pythonjson

解决方案


我觉得你应该:

import json
from genson import SchemaBuilder

builder = SchemaBuilder()
with open(filename, 'r') as f:
    datastore = json.load(f)
    builder.add_object(datastore )

builder.to_schema()

其中文件名是您的文件路径。


推荐阅读