You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何将Cosmos Gremlin输出的嵌套字典转为D3.js可用的层级JSON

Cosmos Gremlin嵌套字典转D3.js层级树JSON实现方案

实现逻辑

输入的嵌套字典每个节点都固定包含key(节点名称)和value(子节点集合)两个属性,我们只需要通过递归遍历每个节点,做字段映射即可:

  • 将原节点的key字段映射为D3树要求的name字段
  • 将原节点value中存储的所有子节点逐一递归转换后,存入D3树要求的children数组中

完整Python代码

import json

def gremlin_to_d3(node):
    # 映射当前节点名称
    d3_node = {"name": node["key"], "children": []}
    # 递归处理所有子节点
    for child_node in node["value"].values():
        d3_node["children"].append(gremlin_to_d3(child_node))
    return d3_node

# 你的输入嵌套字典
input_dict = {
    "Root_Level":{
        "key":"Root_Level",
        "value":{
            "Operation":{
                "key":"Operation",
                "value":{}
            },
            "Technology":{
                "key":"Technology",
                "value":{
                    "Top Management":{
                        "key":"Top Management",
                        "value":{
                            "Associate Product Lead":{
                                "key":"Associate Product Lead",
                                "value":{
                                    "Associate Architect":{
                                        "key":"Associate Architect",
                                        "value":{
                                            "Principal Consultant":{
                                                "key":"Principal Consultant",
                                                "value":{}
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

# 提取根节点并转换
root_node = list(input_dict.values())[0]
d3_tree = gremlin_to_d3(root_node)

# 输出JSON结果,indent参数用于格式化显示
print(json.dumps(d3_tree, indent=2, ensure_ascii=False))

输出验证

运行上述代码后输出的JSON结构和你给出的预期格式完全一致,示例输出如下:

{
  "name": "Root_Level",
  "children": [
    {
      "name": "Operation",
      "children": []
    },
    {
      "name": "Technology",
      "children": [
        {
          "name": "Top Management",
          "children": [
            {
              "name": "Associate Product Lead",
              "children": [
                {
                  "name": "Associate Architect",
                  "children": [
                    {
                      "name": "Principal Consultant",
                      "children": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

扩展说明

如果需要将结果直接保存为JSON文件,可在代码末尾添加以下内容:

with open("d3_tree.json", "w", encoding="utf-8") as f:
    json.dump(d3_tree, f, indent=2, ensure_ascii=False)

内容的提问来源于stack exchange,提问作者ganesh nikumbh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 08:06:03