如何从json文件获取表模式:从json解析表schema?

2024-09-24 00:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用parse_table_schema_from_jsonfrom apache_beam.io.gcp.bigquery import parse_table_schema_from_json获取表模式 来自here

这是我的代码:

 def getSchema(pathToJSON):
    with open(pathToJSON) as data_file:
        schema_data = json.dumps(json.load(data_file))
    table_schema = parse_table_schema_from_json(schema_data)
    # print(table_schema)
    return table_schema

以下是我得到的错误:

^{pr2}$

我的json文件如下所示:

[
  {
    "name": "StockNumber",
    "type": "INTEGER",
    "mode": "NULLABLE"
  },
  {
    "name": "Product",
    "type": "STRING",
    "mode": "NULLABLE"
  }
]

我错过了什么?在


Tags: namefromjsondataparsemodeschemaapache
1条回答
网友
1楼 · 发布于 2024-09-24 00:33:30

你需要的是dict而不是list。像下面提到的那样修改函数和模式文件,然后再试一次

  def getSchema(pathToJSON):
    schema_data = json.dumps(json.load(open("mapping.json")))
    table_schema = parse_table_schema_from_json(schema_data)
    # print(table_schema)
    return table_schema

架构文件应该是

^{pr2}$

相关问题 更多 >