Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
Insert Tree Json into Database

I'm given a JSON (tree structure JSON) and I'm creating an API that will insert all the data at once into the database (organizations with relations). the organisation may have multiple parents and children.

The JSON look's something like this:

{
  "org_name": "organisation",
  "childrens": [
    {
      "org_name": "company tree",
      "childrens": [
        {
          "org_name": "company one"
        },
        {
          "org_name": "company two"
        },
        {
          "org_name": "company three"
        }
      ]
    },
    {
      "org_name": "Big organisation",
      "childrens": [
        {
          "org_name": "company one"
        },
        {
          "org_name": "company two"
        },
        {
          "org_name": "company four"
        },
        {
          "org_name": "company three",
          "childrens": [
            {
              "org_name": "smal farm"
            }
          ]
        }
      ]
    }
  ]
}
 my database schema (which I'm thinking)
 id   |   org_name   |  level  | parent

 *level - tells at which level we are since this is a tree and helps to plot relations

How do I insert these records into the database with their relations? I need to do this in javascript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const maintree = [{
  "org_name": "organisation",
  "childrens": [
    {
      "org_name": "company tree",
      "childrens": [
        {
          "org_name": "company one"
        },
        {
          "org_name": "company two"
        },
        {
          "org_name": "company three"
        }
      ]
    },
    {
      "org_name": "Big organisation",
      "childrens": [
        {
          "org_name": "company one"
        },
        {
          "org_name": "company two"
        },
        {
          "org_name": "company four"
        },
        {
          "org_name": "company three",
          "childrens": [
            {
              "org_name": "smal farm"
            }
          ]
        }
      ]
    }
  ]
}];


function recursiveBuild(tree, level, parent) {
  for(const item of tree) {
        if(item.org_name) {
        let html = document.getElementById(`test`).innerHTML;
            html += `orgname: ${item.org_name}, level: ${level}, parent: ${parent} <br/>`;
        document.getElementById(`test`).innerHTML = html;
        if(item.childrens) {
        recursiveBuild(item.childrens, level+1, item.org_name);
      } else return
    }
  }
}

recursiveBuild(maintree, 0, "");
<div id="test">
</div>

You can use a recursive function like this to insert your records depending on your database. Here I have explained using HTML and added string after each line break for your code to build a flat structure like table.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda