How can I get to the last level of a json nest and format the output dynamically?

I have this json:

[
  {
    "name": "MARVEL",
    "superheroes": "yes"
  },
  {
    "name": "Big Bang Theroy",
    "superheroes": "NO",
    "children": [
      {
        "name": "Sheldon",
        "superheroes": "NO"
      }
    ]
  },
  {
    "name": "dragon ball",
    "superheroes": "YES",
    "children": [
      {
        "name": "goku",
        "superheroes": "yes",
        "children": [
          {
            "name": "gohan",
            "superheroes": "YES"
          }
        ]
      }
    ]
  }
]

I know how to loop and go through it but I need an output like this:

[
  {
    "name": "MARVEL",
    "answer": [
      {
        "there_are_superheroes": "YES"
      }
    ]
  },
  {
    "name": "Big Bang Theroy",
    "answer": [
      {
        "there_are_superheroes": "NO",
        "new_leaft": [
          {
            "name": "sheldon",
            "there_are_superheroes": "NO"
          }
        ]
      }
    ]
  },
  {
    "name": "dragon ball",
    "answer": [
      {
        "there_are_superheroes": "YES",
        "new_leaft": [
          {
            "name": "goku",
            "answer": [
              {
                "there_are_superheroes": "YES",
                "new_leaft": [
                  {
                    "name": "gohan",
                    "answer": [
                      {
                        "there_are_superheroes": "YES"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

I tried something like this:

format(d) {
if (d.children) {
  d.children.forEach((d) => {
    format;
  });
}
}
format(data);

I don't know how to get the structure I want. I have tried to do it with foreach, but at one point I don't know how to dynamically access until the last children, this is an example but I can have n levels where there can be elements with more children. In my real project I am getting a structure from a web service, I need to structure it like this.

Basically this is something like a tree, so I want to put the first level of children inside an attribute called tree

the attribute called superheroes I want it to be shown inside an array called answer and inside of it, there_are_superheroes it will have its value.

 "name": "MARVEL",
 "answer": [
        {
          "there_are_superheroes": "YES",  --> `there_are_superheroes` was `superheroes`,
          "new_leaft": [
                    {
 .
 .

and new_leaft is the equivalent of children

As I said, I know how to go through objects and arrays but in this case, I don't know how to go to the last children nested of each object.



from Recent Questions - Stack Overflow https://ift.tt/30inMNL
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations