skip to Main Content

I need somehelp to create a graph or tree structure from the raw json data from mongoDB, and the data points is parent children related. I tried to create some code to make a data structure and pretty print it, but it still have duplicated rows. Can any one fix my code so that no more duplicated?

Data points:

[
  {
    "_id": {
      "$oid": "577048a0a5f4970b59493861"
    },

    "children": [
      {
        "$oid": "5914d7528ed1755954d6e88f"
      },
      {
        "$oid": "56675f1446e0324b4566f6e9"
      },
      {
        "$oid": "5924f500381e3b5854db8a0c"
      },
      {
        "$oid": "5cc64fadcf995a1d6b54e745"
      },
      {
        "$oid": "5d71b5f49a311e6fd587e3dc"
      },
      {
        "$oid": "599646b5bd0b9864d8e7391d"
      },
      {
        "$oid": "5850ae570b242ff1782ea07c"
      },
      {
        "$oid": "58e9a01f33d3f1f208e032de"
      },
      {
        "$oid": "5546db39dbeafcef63e6efb6"
      },
      {
        "$oid": "5fbdad479a311e6fd58812ff"
      },
      {
        "$oid": "5cabd38142524178c66be1f1"
      },
      {
        "$oid": "5491ddb3d707d710141027c2"
      },
      {
        "$oid": "5d66f0109a311e6fd587e12c"
      },
      {
        "$oid": "56ef0296a7b280e91b475aeb"
      },
      {
        "$oid": "5cd4dabce84cdd76faaf597f"
      },
      {
        "$oid": "58e43b26f79ce47642fae5c4"
      },
      {
        "$oid": "58252929332df05c01c1624f"
      },
      {
        "$oid": "55dfd7d2bb58034a69d563fe"
      },
      {
        "$oid": "56ef03976e418ae84779d827"
      },
      {
        "$oid": "5491dd8bd707d710141027c1"
      },
      {
        "$oid": "5ba2eb071e910a58acf98e3f"
      },
      {
        "$oid": "5cf580fb87823d36e74808bf"
      },
      {
        "$oid": "5dae373a3c6e806fd3bbb1fc"
      },
      {
        "$oid": "61c3a8bf3824dd48d99ab572"
      },
      {
        "$oid": "5a2f468069d12b0a0f4cc68e"
      },
      {
        "$oid": "5be8de44c052dc326ab1e810"
      },
      {
        "$oid": "5c98455267dcef531cbc44f6"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5835f65457eea2852d2bf48c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5924f500381e3b5854db8a0c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "56ef03976e418ae84779d827"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "59c87107cdfaa27f5759b107"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a24977405bdd758991bdb93"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "55aef43d03d4ad2343c2e27f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "56d382a0589377c94614cbbc"
    },

    "children": [
      {
        "$oid": "5cdb32901d30d576fe946d44"
      },
      {
        "$oid": "5a53e1ac23090e4877973532"
      },
      {
        "$oid": "613e86c24faf093afab2c0ea"
      },
      {
        "$oid": "62eb0a4456bdf04c7768843c"
      },
      {
        "$oid": "5a53ddb8159e7944e0b44795"
      },
      {
        "$oid": "606e58ad2f2af36f2219bcc8"
      },
      {
        "$oid": "628da1ce238868408a576c22"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "592776bc8ed1755954d6e8d5"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "594c8252381e3b5854db8af7"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "59503126381e3b5854db8afc"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "58e43b26f79ce47642fae5c4"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "59644a45442b1e826dd7cdac"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5965a855b3e775836dfd8d5b"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "599646b5bd0b9864d8e7391d"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "59965edbf39ca264d9dd41ca"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "58ddc042f67c5f75420cc403"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5914d7528ed1755954d6e88f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a53e1ac23090e4877973532"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5aaf190e159e7944e0b44fe1"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5b15c4adee10e503a6a6d757"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "55c8024609c8cb2176ba55da"
    },

    "children": [
      {
        "$oid": "581bbdfbf0df7f680817970b"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5491dd7ed707d710141027c0"
    },

    "children": [
      {
        "$oid": "5d54d4fc5937ee123ad60c9d"
      },
      {
        "$oid": "5cb7ef9281c9d71d6a82843f"
      },
      {
        "$oid": "5d59c4efa0e40d1239ec5e14"
      },
      {
        "$oid": "5cf707cde5dbb949ad5cc302"
      },
      {
        "$oid": "5d54bd25a0e40d1239ec5d9c"
      },
      {
        "$oid": "5c1a9ae870c07e201b3a4880"
      },
      {
        "$oid": "5c0ec151a9b73222dfdd82d6"
      },
      {
        "$oid": "5de70b4f9a311e6fd587f4e9"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "581bbdfbf0df7f680817970b"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5850ae570b242ff1782ea07c"
    },

    "children": [
      {
        "$oid": "5cd4dabce84cdd76faaf597f"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "54fe27cf5c25b1394be6b303"
    },

    "children": [
      {
        "$oid": "6241210f7210ae3af7727813"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "58252929332df05c01c1624f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5b57b20e8fff7b28249879ae"
    },

    "children": [
      {
        "$oid": "5d01cc1787e6383735ed12a6"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5988de44d4978b0596152387"
    },

    "children": [
      {
        "$oid": "5d06bcaaefa930373488e7c0"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5ad6ca76159e7944e0b45553"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5b15e2dd5240347c64223fed"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "56675f1446e0324b4566f6e9"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a0223cc285a000c2866565c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5491dfa5d707d710141027cb"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5491ddb3d707d710141027c2"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "555560f0ab95c2765d65c3cd"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "55dfd7d2bb58034a69d563fe"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "55cd503216db9f1c385a69db"
    },

    "children": [
      {
        "$oid": "5f4f019d0e170b6f23370b53"
      },
      {
        "$oid": "5a0223cc285a000c2866565c"
      },
      {
        "$oid": "5f6175683c6e806fd3bbcef7"
      },
      {
        "$oid": "6289c9d0adb2f54c79e239d4"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a8211ad159e7944e0b44b67"
    },

    "children": [
      {
        "$oid": "5d82a7063c6e806fd3bbaa37"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5491dd8bd707d710141027c1"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5771fda4d0c907f02785022a"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "57391e8ec1d461090ef7397d"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a53ddb8159e7944e0b44795"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5af905d15240347c64223af3"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5b6cd1aff6056722483d49f2"
    },

    "children": [
      {
        "$oid": "5d531e95ff66e07233a2bf76"
      },
      {
        "$oid": "5d5475e993b2f3707a737d40"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5702f5161c852788280926f3"
    },

    "children": [
      {
        "$oid": "5f0b9d810e170b6f2337073d"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a0dfc4894ba750156a47e9c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "58e9a01f33d3f1f208e032de"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a2472d6f6b76456d074f761"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a2f468069d12b0a0f4cc68e"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5b4e5a958fff7b2824987846"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5546db39dbeafcef63e6efb6"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "56ef0296a7b280e91b475aeb"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "57ce00596dfb923915cf9a7f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5a24b30df6b76456d074f7c4"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5ba2eb071e910a58acf98e3f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5be8de44c052dc326ab1e810"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5be9cc9c20a38c39bc4c807b"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5be9d2553863d639bbface86"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c075bd666a1c95d2acc6a3f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c083f19d011855d2b41d458"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c0ec151a9b73222dfdd82d6"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c1a9ae870c07e201b3a4880"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c3bd69f70c07e201b3a4a99"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c5cd18745f41e78ebfb0732"
    },

    "children": [
      {
        "$oid": "5c5cd1a31279d46f7d90a186"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c5cd1a31279d46f7d90a186"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c609d8686e12578ea359383"
    },

    "children": [
      {
        "$oid": "5d531e95ff66e07233a2bf76"
      },
      {
        "$oid": "5b6cd1aff6056722483d49f2"
      },
      {
        "$oid": "5d5475e993b2f3707a737d40"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c64ccb086e12578ea359499"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c85a3b56ce63b531de0eaef"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5c98455267dcef531cbc44f6"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5caabb5bc8bbd278c0392dab"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cabd38142524178c66be1f1"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cb7ef9281c9d71d6a82843f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cc64fadcf995a1d6b54e745"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cc659bc17629e20e1a66f40"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cd4dabce84cdd76faaf597f"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cdb32901d30d576fe946d44"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cedbb5e87823d36e7480823"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cf580fb87823d36e74808bf"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d01919b90347136e8e233e0"
    },

    "children": [
      {
        "$oid": "5d0191dc87823d36e7480ab6"
      },
      {
        "$oid": "5d898e340e170b6f2336e8f0"
      },
      {
        "$oid": "5d898fc83c6e806fd3bbab51"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d0191dc87823d36e7480ab6"
    },

    "children": [
      {
        "$oid": "5d898e340e170b6f2336e8f0"
      },
      {
        "$oid": "5d898fc83c6e806fd3bbab51"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d01cc1787e6383735ed12a6"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d06bcaaefa930373488e7c0"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d1534be2b4549192d419973"
    },

    "children": [
      {
        "$oid": "5d153c148c044d174fb9b799"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d153c148c044d174fb9b799"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5cf707cde5dbb949ad5cc302"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d531e95ff66e07233a2bf76"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d5475e993b2f3707a737d40"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d54bd25a0e40d1239ec5d9c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d54d460cd8ccb13386abd43"
    },

    "children": [
      {
        "$oid": "5fbdad479a311e6fd58812ff"
      },
      {
        "$oid": "599646b5bd0b9864d8e7391d"
      },
      {
        "$oid": "5924f500381e3b5854db8a0c"
      },
      {
        "$oid": "56675f1446e0324b4566f6e9"
      },
      {
        "$oid": "5914d7528ed1755954d6e88f"
      },
      {
        "$oid": "5d71b5f49a311e6fd587e3dc"
      },
      {
        "$oid": "5d66f0109a311e6fd587e12c"
      },
      {
        "$oid": "5cd4dabce84cdd76faaf597f"
      },
      {
        "$oid": "5546db39dbeafcef63e6efb6"
      },
      {
        "$oid": "5491dd8bd707d710141027c1"
      },
      {
        "$oid": "5cf580fb87823d36e74808bf"
      },
      {
        "$oid": "58252929332df05c01c1624f"
      },
      {
        "$oid": "56ef03976e418ae84779d827"
      },
      {
        "$oid": "55dfd7d2bb58034a69d563fe"
      },
      {
        "$oid": "58e43b26f79ce47642fae5c4"
      },
      {
        "$oid": "577048a0a5f4970b59493861"
      },
      {
        "$oid": "5c98455267dcef531cbc44f6"
      },
      {
        "$oid": "5850ae570b242ff1782ea07c"
      },
      {
        "$oid": "5cc64fadcf995a1d6b54e745"
      },
      {
        "$oid": "56ef0296a7b280e91b475aeb"
      },
      {
        "$oid": "5491ddb3d707d710141027c2"
      },
      {
        "$oid": "5cabd38142524178c66be1f1"
      },
      {
        "$oid": "58e9a01f33d3f1f208e032de"
      },
      {
        "$oid": "5dae373a3c6e806fd3bbb1fc"
      },
      {
        "$oid": "5ba2eb071e910a58acf98e3f"
      },
      {
        "$oid": "5be8de44c052dc326ab1e810"
      },
      {
        "$oid": "5a2f468069d12b0a0f4cc68e"
      },
      {
        "$oid": "61c3a8bf3824dd48d99ab572"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d54d4fc5937ee123ad60c9d"
    },

    "children": [
      {
        "$oid": "5c1a9ae870c07e201b3a4880"
      },
      {
        "$oid": "5d59c4efa0e40d1239ec5e14"
      },
      {
        "$oid": "5de70b4f9a311e6fd587f4e9"
      },
      {
        "$oid": "5c0ec151a9b73222dfdd82d6"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d59c4efa0e40d1239ec5e14"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d66f0109a311e6fd587e12c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d66f12d2f2af36f22197fed"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d66f1cb3c6e806fd3bba448"
    },

    "children": [
      {
        "$oid": "5d66f12d2f2af36f22197fed"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d71b5f49a311e6fd587e3dc"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d82a7063c6e806fd3bbaa37"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d898e340e170b6f2336e8f0"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5d898fc83c6e806fd3bbab51"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5dae373a3c6e806fd3bbb1fc"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5de70b4f9a311e6fd587f4e9"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5e44aee59a311e6fd587fb53"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5e4dc8010e170b6f2336fd33"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5e9e56889a311e6fd587ff9c"
    },

    "children": [
      {
        "$oid": "59503126381e3b5854db8afc"
      },
      {
        "$oid": "5be9cc9c20a38c39bc4c807b"
      },
      {
        "$oid": "5c083f19d011855d2b41d458"
      },
      {
        "$oid": "5cc659bc17629e20e1a66f40"
      },
      {
        "$oid": "5491dfa5d707d710141027cb"
      },
      {
        "$oid": "5cedbb5e87823d36e7480823"
      },
      {
        "$oid": "5be9d2553863d639bbface86"
      },
      {
        "$oid": "581bbdfbf0df7f680817970b"
      },
      {
        "$oid": "5a24b30df6b76456d074f7c4"
      },
      {
        "$oid": "594c8252381e3b5854db8af7"
      },
      {
        "$oid": "55c8024609c8cb2176ba55da"
      },
      {
        "$oid": "59965edbf39ca264d9dd41ca"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5ed02be39a311e6fd58802d6"
    },

    "children": [
      {
        "$oid": "57391e8ec1d461090ef7397d"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5edd66c79a311e6fd5880363"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5f0b9d810e170b6f2337073d"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5f4f019d0e170b6f23370b53"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5f6175683c6e806fd3bbcef7"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5fbdad479a311e6fd58812ff"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "5fe3bd7a3c6e806fd3bbd9f5"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "60187c220e170b6f23371a5e"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "606e58ad2f2af36f2219bcc8"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "613e86c24faf093afab2c0ea"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "61c3a8bf3824dd48d99ab572"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "61c3b11a4faf093afab2c86e"
    },

    "children": [
      {
        "$oid": "5a24977405bdd758991bdb93"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "61c3b3e83824dd48d99ab580"
    },

    "children": [
      {
        "$oid": "5c3bd69f70c07e201b3a4a99"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "61c3b84d7d28ec48d8455710"
    },

    "children": [
      {
        "$oid": "5a2472d6f6b76456d074f761"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "61c3bb467d28ec48d8455714"
    },

    "children": [
      {
        "$oid": "5965a855b3e775836dfd8d5b"
      }
    ],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "6241210f7210ae3af7727813"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "6289c9d0adb2f54c79e239d4"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "628da1ce238868408a576c22"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "6298322c238868408a576cc5"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "62eb0a4456bdf04c7768843c"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "639643faf06d0004d42b2457"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "63a23108a4243f051c006f77"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "64090ff9903a4604d5e69032"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "64518b0da4243f051c007932"
    },

    "children": [],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "64924d566d951e05091e295d"
    },

    "children": [],
    "kind": "Organisation"
  }
]

Need to write a script with any language to build a tree from this data, but need to make sure there is no duplicated. But my current code wont work this way:

const data = require("./dat.json");

function createTree(data, rootId) {
  const root = data.find((item) => item._id.$oid === rootId);
  if (!root) return null;

  const children = root.children.map((child) => createTree(data, child.$oid));

  return {
    ...root,
    children,
  };
}

function prettyPrint(nodes, level = 0) {
  nodes.forEach((node) => {
    console.log(`${"  ".repeat(level)} (${node.kind}) ${node._id.$oid}`);
    prettyPrint(node.children, level + 2);
  });
}

const forest = data.map((item) => createTree(data, item._id.$oid));

prettyPrint(forest);

You can see the duplicated rows, i dont want those duplicated! logically there won’t have any duplicated.

enter image description here

2

Answers


  1. You can try with this python code:

    import json
    
    with open('dat.json') as f:
        data = json.load(f)
    
    id_to_node = {node['_id']['$oid']: node for node in data}
    
    all_ids = set(id_to_node.keys())
    
    children_ids = set()
    
    for node in data:
        for i, child in enumerate(node.get("children", [])):
            child_id = child['$oid']
            if child_id in id_to_node:  # If child node exists
                node['children'][i] = id_to_node[child_id]
                children_ids.add(child_id)
    
    root_ids = all_ids - children_ids
    
    new_data = [id_to_node[root_id] for root_id in root_ids]
    
    def pprint(node, ind=0):
        ind_str = " "*4*ind
        print(f"{ind_str}({node.get('kind', '')}) {node['_id']['$oid']}: ")
        for child in node.get("children", []):
            pprint(child, ind+1)
    
    
    for node in new_data:
        pprint(node)
    

    You need to build structure and later filter out all nodes that are mark as child.
    Then you can pretty print

    Login or Signup to reply.
  2. The problem is that your forest will get a node for each item in data. You should only have top-level entries in the forest array. To know which they are, you first have to check which nodes have no parent. This you can do while building the nested object structure.

    So adapt your createTree function, so it only takes the data as argument, and it returns the array of root nodes.

    I would also avoid having to scan the data with find each time you need to look up an id. Use a hashmap for that (could be a plain object in JavaScript).

    Like this:

    function createTree(data) {
        const nodes = Object.fromEntries(data.map(item  => 
            [item._id.$oid, { ...item, children: [] }]
        ));
        const roots = new Set(Object.values(nodes));
        for (const {_id, children} of data) {
            nodes[_id.$oid].children.push(...children.map(({$oid}) => {
                roots.delete(nodes[$oid]);
                return nodes[$oid];
            }));
        }
        return [...roots];
    }
    

    Call like this:

    const forest = createTree(data);
    prettyPrint(forest);
    

    (No change is needed in prettyPrint).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search