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

232
Visualizações
Extracting Content when Multiple Children with Same Name (Google Scripts JS)

I'm not sure how to extract content when there are multiple optional children with the same name.

  1. Individuals can have multiple children with the same name. e.g. user_defined_text_field below.
  2. Some individuals may not have any children.

Hopefully the example below provides the relevant context.

FORM OF DATA

(From an API from a service)

<api>
 <response>
      <service>api_search</service>
      <individuals count="1">
          <individual id="1">
                    <first_name>James</first_name>
                    <last_name>Jones</last_name>
                    <email>jjones@gmail.com</email>
                    <gender>M</gender>
                    <user_defined_text_fields>
                         <user_defined_text_field>
                              <name>udf_text_1</name>
                              <label>Fav Color</label>
                              <text>Blue</text> // ****** I WANT THIS ******
                              <admin_only>false</admin_only>
                         </user_defined_text_field>
                         <user_defined_text_field>
                              <name>udf_text_3</name>
                              <label>Area</label>
                              <text>Urban</text>
                              <admin_only>false</admin_only>
                         </user_defined_text_field>
                    </user_defined_text_fields>
          </individual>               
      </individuals>
 </response>

CURRENT PORTION OF CODE TO EXTRACT RELEVANT DATA

(Inside Google Script. Everything is working perfect -- I only need help with the line I have commented below.)

// ...

const entries = fetch.getRootElement().getChild('response').getChild('individuals').getChildren();
const list = new Array();

for (let i in entries) { 

  const first_name = entries[i].getChildText('first_name');
  const last_name = entries[i].getChildText('last_name');
  const email = entries[i].getChildText('email');
  const gender = entries[i].getChildText('gender');


  const fav_color = ???  // ****** NEED HELP HERE ******


  list.push([first_name, last_name, email, gender]);
};


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

0

Description

Since user_defined_text_fields contains an array of user_defined_text_field, you have to loop through the array of children to get to the one that has the color.

Script

for (let i in entries) { 

  var first_name = entries[i].getChildText('first_name');
  var last_name = entries[i].getChildText('last_name');
  var email = entries[i].getChildText('email');
  var gender = entries[i].getChildText('gender');
  var fields = entries[i].getChild("user_defined_text_fields");
  var children = fields.getChildren("user_defined_text_field");
  if( children ) {
    for( var j=0; j<children.length; j++ ) {
      if( children[j].getChildText("label") === "Fav Color" ) {
        var fav_color = children[j].getChildText("text");
        break;
      }
    }
  }
}

Reference

  • https://developers.google.com/apps-script/reference/xml-service/element#getChildren(String)
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