Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!