JSON到树形图

时间:2018-06-28 11:52:00

标签: python json dictionary

我有一个严重嵌套的JSON对象。有没有一种方法可以查看分层树图?我经历了一些资源,例如Pydot,Plotly等,但是没有什么可以呈现我格式的JSON。

JSON文件:

{
  "found_intents": {
    "_DATE": {}
  },
  "sentence": "What is your name",
  "tree": [
    [
      {
        "canonical": null,
        "concept": "_START_TAG",
        "correct_string": "<start>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<start>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<start>",
            "language": "english",
            "span": [
              0,
              1
            ],
            "span_string": "<start>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<start>",
        "language": "english",
        "span": [
          0,
          1
        ],
        "span_string": "<start>",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_WHAT_IS",
        "correct_string": "what is",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "what",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "what",
            "language": "english",
            "span": [
              1,
              2
            ],
            "span_string": "what",
            "weight": 1.0
          },
          {
            "canonical": null,
            "concept": "",
            "correct_string": "is",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "is",
            "language": "english",
            "span": [
              2,
              3
            ],
            "span_string": "is",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "what is",
        "language": "english",
        "span": [
          1,
          3
        ],
        "span_string": "what is",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_DICTIONARY",
        "correct_string": "your",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "your",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "your",
            "language": "english",
            "span": [
              3,
              4
            ],
            "span_string": "your",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "your",
        "language": "english",
        "span": [
          3,
          4
        ],
        "span_string": "your",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_DICTIONARY",
        "correct_string": "name",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "name",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "name",
            "language": "english",
            "span": [
              4,
              5
            ],
            "span_string": "name",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "name",
        "language": "english",
        "span": [
          4,
          5
        ],
        "span_string": "name",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_END_TAG",
        "correct_string": "<end>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<end>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<end>",
            "language": "english",
            "span": [
              5,
              6
            ],
            "span_string": "<end>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<end>",
        "language": "english",
        "span": [
          5,
          6
        ],
        "span_string": "<end>",
        "weight": 1.0
      }
    ],
    [
      {
        "canonical": null,
        "concept": "_START_TAG",
        "correct_string": "<start>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<start>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<start>",
            "language": "english",
            "span": [
              0,
              1
            ],
            "span_string": "<start>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<start>",
        "language": "english",
        "span": [
          0,
          1
        ],
        "span_string": "<start>",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_WHAT_IS",
        "correct_string": "what is",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "what",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "what",
            "language": "english",
            "span": [
              1,
              2
            ],
            "span_string": "what",
            "weight": 1.0
          },
          {
            "canonical": null,
            "concept": "",
            "correct_string": "is",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "is",
            "language": "english",
            "span": [
              2,
              3
            ],
            "span_string": "is",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "what is",
        "language": "english",
        "span": [
          1,
          3
        ],
        "span_string": "what is",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_DICTIONARY",
        "correct_string": "your",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "your",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "your",
            "language": "english",
            "span": [
              3,
              4
            ],
            "span_string": "your",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "your",
        "language": "english",
        "span": [
          3,
          4
        ],
        "span_string": "your",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_THEATRE_ID",
        "correct_string": "name",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "name",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "name",
            "language": "english",
            "span": [
              4,
              5
            ],
            "span_string": "name",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "name",
        "language": "english",
        "span": [
          4,
          5
        ],
        "span_string": "name",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_END_TAG",
        "correct_string": "<end>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<end>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<end>",
            "language": "english",
            "span": [
              5,
              6
            ],
            "span_string": "<end>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<end>",
        "language": "english",
        "span": [
          5,
          6
        ],
        "span_string": "<end>",
        "weight": 1.0
      }
    ],
    [
      {
        "canonical": null,
        "concept": "_START_TAG",
        "correct_string": "<start>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<start>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<start>",
            "language": "english",
            "span": [
              0,
              1
            ],
            "span_string": "<start>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<start>",
        "language": "english",
        "span": [
          0,
          1
        ],
        "span_string": "<start>",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_WHAT_IS",
        "correct_string": "what is",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "what",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "what",
            "language": "english",
            "span": [
              1,
              2
            ],
            "span_string": "what",
            "weight": 1.0
          },
          {
            "canonical": null,
            "concept": "",
            "correct_string": "is",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "is",
            "language": "english",
            "span": [
              2,
              3
            ],
            "span_string": "is",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "what is",
        "language": "english",
        "span": [
          1,
          3
        ],
        "span_string": "what is",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_THEATRE_ID",
        "correct_string": "your",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "your",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "your",
            "language": "english",
            "span": [
              3,
              4
            ],
            "span_string": "your",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "your",
        "language": "english",
        "span": [
          3,
          4
        ],
        "span_string": "your",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_DICTIONARY",
        "correct_string": "name",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "name",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "name",
            "language": "english",
            "span": [
              4,
              5
            ],
            "span_string": "name",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "name",
        "language": "english",
        "span": [
          4,
          5
        ],
        "span_string": "name",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_END_TAG",
        "correct_string": "<end>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<end>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<end>",
            "language": "english",
            "span": [
              5,
              6
            ],
            "span_string": "<end>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<end>",
        "language": "english",
        "span": [
          5,
          6
        ],
        "span_string": "<end>",
        "weight": 1.0
      }
    ],
    [
      {
        "canonical": null,
        "concept": "_START_TAG",
        "correct_string": "<start>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<start>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<start>",
            "language": "english",
            "span": [
              0,
              1
            ],
            "span_string": "<start>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<start>",
        "language": "english",
        "span": [
          0,
          1
        ],
        "span_string": "<start>",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_WHAT_IS",
        "correct_string": "what is",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "what",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "what",
            "language": "english",
            "span": [
              1,
              2
            ],
            "span_string": "what",
            "weight": 1.0
          },
          {
            "canonical": null,
            "concept": "",
            "correct_string": "is",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "is",
            "language": "english",
            "span": [
              2,
              3
            ],
            "span_string": "is",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "what is",
        "language": "english",
        "span": [
          1,
          3
        ],
        "span_string": "what is",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_THEATRE_ID",
        "correct_string": "your",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "your",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "your",
            "language": "english",
            "span": [
              3,
              4
            ],
            "span_string": "your",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "your",
        "language": "english",
        "span": [
          3,
          4
        ],
        "span_string": "your",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_THEATRE_ID",
        "correct_string": "name",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "name",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "name",
            "language": "english",
            "span": [
              4,
              5
            ],
            "span_string": "name",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "name",
        "language": "english",
        "span": [
          4,
          5
        ],
        "span_string": "name",
        "weight": 1.0
      },
      {
        "canonical": null,
        "concept": "_END_TAG",
        "correct_string": "<end>",
        "definition": "",
        "details": [
          {
            "canonical": null,
            "concept": "",
            "correct_string": "<end>",
            "definition": "",
            "details": [],
            "e.g.": [],
            "grammar": "<end>",
            "language": "english",
            "span": [
              5,
              6
            ],
            "span_string": "<end>",
            "weight": 1.0
          }
        ],
        "e.g.": [],
        "grammar": "<end>",
        "language": "english",
        "span": [
          5,
          6
        ],
        "span_string": "<end>",
        "weight": 1.0
      }
    ]
  ]
}

更准确地说,我想提取每个级别的平均分支数量。

预期结果将是:

Level 0: 4  
Level 1: (1 + 2 + 1 + 1)/4  
Level 2: 0

1 个答案:

答案 0 :(得分:0)

尚不清楚您要做什么,但是以下代码计算每个嵌套深度的字典和列表数。我们使用collections.deque作为堆栈来执行广度优先搜索,计算每个级别上嵌套容器的数量,将计数存储在defaultdict列表中,然后将这些容器推入堆栈以备将来使用处理。完成对所有对象的计数后,我们将计算每个级别的平均分支数量。

我不会将您的数据粘贴到此代码中,因为它的长度约为740行。我将这些数据称为data_string,在我的机器上,我只是将您的数据用三引号引起来,但是您当然可以将其保存为文件,并使用json.load进行加载。

import json
from collections import defaultdict, deque

data = json.loads(data_string)

def get_branches(obj):
    branches = defaultdict(list)
    stack = deque()
    stack.append((obj, 0))
    while stack:
        obj, depth = stack.pop()
        newdepth = depth + 1
        branch_count = 0
        if isinstance(obj, dict):
            obj = obj.values()
        for child in obj:
            if isinstance(child, (list, dict)):
                branch_count += 1
                stack.append((child, newdepth))
        if branch_count:
            branches[depth].append(branch_count)
    return branches

branches = get_branches(data)
for depth in sorted(branches.keys()):
    row = branches[depth]
    mean = sum(row) / len(row) if row else None
    print('Level', depth, row, mean)

输出

Level 0 [2] 2.0
Level 1 [4, 1] 2.5
Level 2 [5, 5, 5, 5] 5.0
Level 3 [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] 3.0
Level 4 [1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1] 1.2
Level 5 [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] 3.0