简体   繁体   English

Python:如何从 python 中的 JSON 模式中检索表名和相应的列名

[英]Python: How to retrieve table name and corresponding columns name from JSON schema in python

Sample Schema:示例架构:

{
"CLASS_A" :  {
      "type": "object",
       "properties": {
           "CLASS_B": {
             "type": "array",
             "items": {
                 "type": "object",
                  "properties": {
                     "NAME1": {
                          "type":  "string",
                       },
                     "NAME2": {
                          "type":  "string",
                       },
                     "CLASS_C":  {
                          "type": "array",
                          "items": {
                              "type": "object",
                              "properties": {
                                   "NAME3": {
                                       "type":  "string",
                                    },
                                   "NAME4": {
                                       "type":  "string",
                                   },
                                   
                                },
                            },
                        },  
                    },  
                },
            },
        },
    },  
},  

CLASS_B and CLASS_C are the table names and NAME1 and NAME2 are the column names of the table CLASS_B CLASS_B 和 CLASS_C 是表名,NAME1 和 NAME2 是表 CLASS_B 的列名

I have a big JSON schema with around 600 tables....我有一个大的 JSON 架构,大约有 600 个表....

I need to automatically retrieve table names and their corresponding columns in one place.我需要在一个地方自动检索表名及其对应的列。

First, you change it to a dictionary with command首先,您将其更改为带有命令的字典

import json
with open('data.json') as json_file:
    data = json.load(json_file)

Then you read it as a dictionary with:然后您将其作为字典阅读:

data['CLASS_A']['properties']
...

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

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