Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
How can i traverse a complex multi level json string to create objects in Angularjs or Javascript?

I have a Json string like this:

 var json =     "[{"Id":"1234",
        "FirstName":"One",    
        "Index":"32",
        "Type":"t1",
        "Children":[{"Id":"976","FirstName":"Two","Index":"32","Type":"t2",     

           "Children":[{"Id":"428",
               "FirstName":"Three",            
               "Index":"32",
               "Type":"t3",
               "Children":[],
               "ParentId":"f235"}],        
               
        "ParentId":"f826"}],
     "ParentId":"0000"}]"

I want to create a number of objects with only the fields Id,FirstName,Children, not Index and Type. Each Object has a number of child Services as well.

So obj1 will have properties Firstname, ParentId and Children and if you drill down to its Children it will have another object with Id , firstname , ParentId , and for this if we drill down there are no more Children

I began by doing something like 
    
    var servicejson = JSON.parse(jsonStr);
     for (let i = 0; i < servicejson).length; i++) {
            var parentnode = {
                Id: servicejson[i].Id,
                FirstName: servicejson[i].JsonValue,        
                ParentId: servicejson[i].ParentId,             
            };        
            
            //check if children
            if (servicejson[i].Children.length > 0) {
                //do something here ??
            }
    }
    

There will be one Object and within that will be a Child , and withing that Child there is another Child node. I got stuck assigning these to the Object or pushing onto an array on the Object as I assume it may need a recursive call to assign the Children array and this is where i need some help Any ideas on how I can to do this ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can do something like this

 var json =     [{"Id":"1234",
        "FirstName":"One",    
        "Index":"32",
        "Type":"t1",
        "Children":[{"Id":"976","FirstName":"Two","Index":"32","Type":"t2",     

           "Children":[{"Id":"428",
               "FirstName":"Three",            
               "Index":"32",
               "Type":"t3",
               "Children":[],
               "ParentId":"f235"}],        
               
        "ParentId":"f826"}],
     "ParentId":"0000"}]


const transform = ({Id, FirstName, Children}) => ({
  Id,
  FirstName,
  Children: Children.map(transform)
})

const result = json.map(transform)

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda