Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert nested Json to flat Json with parentId to every node

The following Json structure is a result of Neo4J apoc query. I want to convert this nested Json to flat Json structure as shown in the second json.

[
    {
      "child1": [
        {
          "_type": "EntityChild1",
          "name": "Test222",
          "_id": 2
        }
      ],
      "child2": [
        {
          "_type": "EntityChild2",
          "name": "Test333",
          "_id": 3,
          "child2_child1": [
            {
              "_type": "EntityChild2_1",
              "name": "Test444",
              "_id": 6,
              "child2_child1_child1": [
                {
                  "_type": "EntityChild2_1_1",
                  "name": "Test555",
                  "_id": 7
                }
              ]
            }
          ]
        }
      ],
      "_type": "EntityParent",
      "name": "Test000",
      "_id": 1,
      "child3": [
        {
          "_type": "EntityChild3",
          "name": "Test111",
          "_id": 4
        }
      ],
      "child4": [
        {
          "_type": "EntityChild4",
          "name": "Test666",
          "_id": 5
        }
      ]
    }
    ]

This is the result i am looking for, I also want the parentId appended to every node. If no parent is there for a particular node then it should have parentid as -1.

[
  {
    "_type": "EntityParent",
    "name": "Test000",
    "_id": 1,
    "parentid": -1
  },
  {
    "_type": "EntityChild1",
    "name": "Test222",
    "_id": 2,
    "parentid": 1
  },
  {
    "_type": "EntityChild2",
    "name": "Test333",
    "_id": 3,
    "parentid": 1
  },
  {
    "_type": "EntityChild2_1",
    "name": "Test444",
    "_id": 6,
    "parentid": 3
  },
  {
    "_type": "EntityChild2_1_1",
    "name": "Test555",
    "_id": 7,
    "parentid": 6
  },
  {
    "_type": "EntityChild3",
    "name": "Test111 ",
    "_id": 4,
    "parentid": 1
  },
  {
    "_type": "EntityChild4",
    "name": "Test666",
    "_id": 5,
    "parentid": 1
  }
]

Let me know if any further information is required.

like image 524
Akshay Kumar Avatar asked Apr 07 '26 18:04

Akshay Kumar


1 Answers

You could take an iterative and recursive approach by using a function which takes an array and a parent id for the actual level.

If a property starts with child, it calls the function again with the actual _id and pushes all items to the result set.

function getFlat(array, parentid) {
    return array.reduce((r, o) => {
        var temp = {};
        r.push(temp);
        Object.entries(o).forEach(([k, v]) => {
            if (k.startsWith('child')) {
                r.push(...getFlat(v, o._id));
            } else {
                temp[k] = v;
            }
        });
        temp.parentid = parentid;
        return r;
    }, []);
}


var data = [{ child1: [{ _type: "EntityChild1", name: "Test222", _id: 2 }], child2: [{ _type: "EntityChild2", name: "Test333", _id: 3, child2_child1: [{ _type: "EntityChild2_1", name: "Test444", _id: 6, child2_child1_child1: [{ _type: "EntityChild2_1_1", name: "Test555", _id: 7 }] }] }], _type: "EntityParent", name: "Test000", _id: 1, child3: [{ _type: "EntityChild3", name: "Test111", _id: 4 }], child4: [{ _type: "EntityChild4", name: "Test666", _id: 5 }] }],
    flat = getFlat(data, -1);

console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 100
Nina Scholz Avatar answered Apr 10 '26 06:04

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!