【问题标题】:Generate JSON schema in Python在 Python 中生成 JSON 模式
【发布时间】:2017-05-24 06:21:53
【问题描述】:

我正在从事一个从 Web 服务中提取数据的项目。我想分析我从这些不同的调用中得到的 JSON 响应,以便我能够理解我得到的响应的结构。

例如,查看此响应提供的 json:https://jsonplaceholder.typicode.com/users

我希望生成此响应的“模式”或骨架,如下所示:

[
  {
    "id": "Number",
    "name": "String",
    "username": "String",
    "email": "String",
    "address": {
      "street": "String",
      "suite": "String",
      "city": "String",
      "zipcode": "String",
      "geo": {
        "lat": "Number",
        "lng": "Number"
      }
    },
    "phone": "String",
    "website": "String",
    "company": {
      "name": "String",
      "catchPhrase": "String",
      "bs": "String"
    }
]

有谁知道我可以使用现有的标准或第 3 方模块来解决这个问题吗?我进行了大量的搜索,但没有任何运气。

提前感谢您的任何建议。

【问题讨论】:

    标签: python json web-services


    【解决方案1】:

    我不知道有一种简单的方法可以让您使用现有的标准或第 3 方模块获得所需的数据结构。

    作为一个想法 - 由于没有其他回复 - 您也许可以尝试类似于以下内容,但要更加小心地遍历嵌套字典:

    for i in range(len(json_parsed)):
        for k,v in json_parsed[i].items():
            print(k, str(type(v)).replace("<class", "").replace('>',""))
    
        id  'int'
        username  'str'
        website  'str'
        address  'dict'
        email  'str'
        phone  'str'
        company  'dict'
        name  'str'
        id  'int'
        username  'str'
        website  'str'
        address  'dict'
        email  'str'
        phone  'str'
        company  'dict'
        name  'str'
    

    【讨论】:

      【解决方案2】:

      这是部分解决方案。它遍历了一个 Python 字典,当你这样做时你得到的那种:

      data = requests.get(...)
      results = data.json()
      

      我仍在努力,即深入检查列表的复杂元素是否相同,以便我可以返回 first... 而不是 first, second

      代码如下:

      def schema(d, spacer='  ', indent=0, force=0):
          types = [list, set, dict, tuple]
          open_symbols = ['[', '{', '{', '(']
          close_symbols = [']', '}', '}', ')']
          res = ''
          typ = type(d)
          if typ in types:
              os, cs = list(zip(open_symbols, close_symbols))[types.index(typ)]
              res += f'{spacer * (indent*force)}{os}\n'
              if isinstance(d, dict):
                  for key, value in d.items():
                      res += f'{spacer * (indent+1)}{key}: '
                      res += f'{schema(value, spacer, indent+1)}\n'
              else:
                  if(typ is set):
                      return res[:-1] + (d.pop().__class__.__name__ if len(d) else None) + cs
                  else:
                      if all(isinstance(x, type(d[0])) for x in d[1:]) and type(d[0]) not in types:
                          return res[:-1] + d[0].__class__.__name__ + cs
                      else:
                          for value in d:
                              res += f'{schema(value, spacer, indent+1, 1)}\n'
              res += f'{spacer * indent}{cs}'
          else:
              res = d.__class__.__name__
          return res
      

      乍一看,很明显:

      1. 这是源自我自己的pprint 版本,所以你有间距元素;我不喜欢pprint 的输出;
      2. 这并不是 JSON 结果所独有的,这就是为什么您会在其中找到 settuple 的原因;许多其他结构化类型没有被考虑在内;
      3. 这会返回一个str,所以你可以这样使用它:print(schema(d)),其中d是一些Python变量;
      4. 还有工作要做,即所有那些returns;

      我希望这会有所帮助,经过这么多年。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-08
        • 2016-05-15
        • 2021-12-05
        • 1970-01-01
        • 2014-06-27
        • 2017-02-22
        • 2018-06-30
        • 2019-06-23
        相关资源
        最近更新 更多