d3-mitch-tree icon indicating copy to clipboard operation
d3-mitch-tree copied to clipboard

Converting JSON hierachy data to D3 tree hierarchy

Open gkhatwani opened this issue 2 years ago • 1 comments

I am trying to create d3 BoxedTree for which I need hierarchical tree in slightly different format. I have input data as below. (First Format):

{
  "Parent": "Enterprise Risk Management Framework",
  "Type": "Root",
  "url": "",
  "Child": "Risk Governance Framework",
  "description": "Enterprise Risk Management Framework",
  "id": 0,
  "child_id": 3,
  "children": [
    {
      "Parent": "Risk Governance Framework",
      "Type": "Stem",
      "url": "",
      "Child": "Wholesale Credit Risk Framework",
      "description": "Risk Governance Framework",
      "id": 3,
      "child_id": 4,
      "children": [
        {
          "Parent": "Wholesale Credit Risk Framework",
          "Type": "Stem",
          "url": "",
          "Child": "Wholesale Credit Risk Policy",
          "description": "Wholesale Credit Risk Framework",
          "id": 4,
          "child_id": 6,
          "children": [
            {
              "Parent": "Wholesale Credit Risk Policy",
              "Type": "Stem",
              "url": "",
              "Child": "Wholesale Credit In-Business Quality Assurance Standard",
              "description": "Wholesale Credit Risk Policy",
              "id": 6,
              "child_id": 7,
              "children": [
                {
                  "Parent": "Wholesale Credit In-Business Quality Assurance Standard",
                  "Type": "Leaf",
                  "url": "",
                  "description": "Wholesale Credit In-Business Quality Assurance Standard",
                  "id": 7
                }
              ]
            },
            {
              "Parent": "Wholesale Credit Risk Policy",
              "Type": "Stem",
              "url": "",
              "Child": "WCR Exception Management Standard",
              "description": "Wholesale Credit Risk Policy",
              "id": 6,
              "child_id": 8,
              "children": [
                {
                  "Parent": "WCR Exception Management Standard",
                  "Type": "Leaf",
                  "url": "",
                  "description": "WCR Exception Management Standard",
                  "id": 8
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "Parent": "Risk Governance Framework",
      "Type": "Stem",
      "url": "",
      "Child": "Global Collateral Management Policy",
      "description": "Risk Governance Framework",
      "id": 3,
      "child_id": 9,
      "children": [
        {
          "Parent": "Global Collateral Management Policy",
          "Type": "Stem",
          "url": "",
          "Child": "WCR Collateral Management Standard",
          "description": "Global Collateral Management Policy",
          "id": 9,
          "child_id": 10,
          "children": [
            {
              "Parent": "WCR Collateral Management Standard",
              "Type": "Leaf",
              "url": "",
              "description": "WCR Collateral Management Standard",
              "id": 10
            }
          ]
        }
      ]
    },
    {
      "Parent": "Risk Governance Framework",
      "Type": "Stem",
      "url": "",
      "Child": "Real Estate Appraisal and Valuation Policy",
      "description": "Risk Governance Framework",
      "id": 3,
      "child_id": 11,
      "children": [
        {
          "Parent": "Real Estate Appraisal and Valuation Policy",
          "Type": "Stem",
          "url": "",
          "Child": "Commercial Real Estate Appraisal/Valuation Standard",
          "description": "Real Estate Appraisal and Valuation Policy",
          "id": 11,
          "child_id": 12,
          "children": [
            {
              "Parent": "Commercial Real Estate Appraisal/Valuation Standard",
              "Type": "Stem",
              "url": "",
              "Child": "Commercial Real Estate Appraisal/Valuation Procedures",
              "description": "Commercial Real Estate Appraisal/Valuation Standard",
              "id": 12,
              "child_id": 13,
              "children": [
                {
                  "Parent": "Commercial Real Estate Appraisal/Valuation Procedures",
                  "Type": "Leaf",
                  "url": "",
                  "description": "Commercial Real Estate Appraisal/Valuation Procedures",
                  "id": 13
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Expected JSON hierarch format for creating d3 BoxedTree. (Second Format)

{
					"id": 0,
					"name": "Enterprise Risk Management Framework",
					"type": "Root",
					"description": "Enterprise Risk Management Framework",
					"url":  "",
					"children": [
						{
							"id": 3,
							"name": "Risk Governance Framework",
							"type": "Stem",
							"description": "Risk Governance Framework",
							"url":  "",
							"children": [
								{
									"id": 4,
									"name": "Wholesale Credit Risk Framework",
									"type": "Stem",
									"description": "Wholesale Credit Risk Framework",
									"url":  "",
									"children": [
										{
											"id": 6,
											"name": "Wholesale Credit Risk Policy",
											"type": "Stem",
											"description": "Wholesale Credit Risk Policy",
											"url":"",
											"children": [
											{
												"id": 7,
												"name": "Wholesale Credit In-Business Quality Assurance Standard",
												"type": "Leaf",
												"description": "Wholesale Credit In-Business Quality Assurance Standard",
												"url":"",
												"children": []
											
											},
											{
												"id": 8,
												"name": "WCR Exception Management Standard",
												"type": "Leaf",
												"description": "WCR Exception Management Standard",
												"url":"",
												"children": []
											
											}
											
											
											]
										}
									]
								},
								{
									"id": 9,
									"name": "Global Collateral Management Policy",
									"type": "Stem",
									"description": "Global Collateral Management Policy",
									"url":"",
									"children": [
										{
											"id": 10,
											"name": "WCR Collateral Management Standard",
											"type": "Leaf",
											"description": "WCR Collateral Management Standard",
											"url":"",
											"children": []
										}
									]
								},
								{
									"id": 11,
									"name": "Real Estate Appraisal and Valuation Policy",
									"type": "Stem",
									"description": "Real Estate Appraisal and Valuation Policy",
									"url":"",
									"children": [
										{
											"id": 12,
											"name": "Commercial Real Estate Appraisal/Valuation Standard",
											"type": "Stem",
											"description": "Commercial Real Estate Appraisal/Valuation Standard",
											"url":"",
											"children": [
												{
													"id": 13,
													"name": "Commercial Real Estate Appraisal/Valuation Procedures",
													"type": "Leaf",
													"description": "Commercial Real Estate Appraisal/Valuation Procedures",
													"url":"",
													"children": []
												}
											]
										}
									]
								}
							
								
							]
						}
					]
				}

Can someone help to know how to transform first format to second format using javascript?

gkhatwani avatar Mar 09 '22 22:03 gkhatwani

You can do yourself so I did for my project. I did the "trasformation" in language backend like JAVA.

mrk1989 avatar Mar 03 '23 17:03 mrk1989