title: How can we flatten XML parsed with fast-xml-parser
body: <p>Have inherited a Node.js app that needs some maintenance and its not my strong point.</p>
  
  <p>We are parsing XML using fast-xml-parser which works really well with most of our inputs.  However we have some inputs that are an extra level deep, and we need to flatten the output to all be same level.</p>
  
  <p>The Input:  where Price value is the extra level deep</p>
  
  <pre><code>&lt;products capture-installed="true"&gt;
  &lt;script/&gt;
  &lt;script/&gt;
  &lt;script/&gt;
  &lt;product&gt;
  &lt;pid&gt;8&lt;/pid&gt;
  &lt;modelno&gt;6273033&lt;/modelno&gt;
  &lt;name&gt;
  &lt;![CDATA[ Big Red Truck ]]&gt;
  &lt;/name&gt;
  &lt;category&gt;
  &lt;![CDATA[ Toys]]&gt;
  &lt;/category&gt;
  &lt;currency&gt;USD&lt;/currency&gt;
  &lt;price&gt;
    &lt;actualprice&gt;19.20&lt;/actualprice&gt;
  &lt;/price&gt;
  &lt;/product&gt;
  </code></pre>
  
  <p>When we flatten it with existing code we get:</p>
  
  <pre><code>   "product": {
        "pid": "8",
        "modelno": "6273033",
        "name": "Big Red Truck",
        "category": "Toys",
        "currency": "USD",
        "price": {
           "actualprice": "19.20"
        }
  </code></pre>
  
  <p>But what we need is something like:</p>
  
  <pre><code>   "product": {
        "pid": "8",
        "modelno": "6273033",
        "name": "Big Red Truck",
        "category": "Toys",
        "currency": "USD",
        "price-actualprice": "19.20"
        }
  </code></pre>
  
  <p>The current Code:</p>
  
  <pre><code>const parse = require("fast-xml-parser");
  
  const options = {
    ignoreAttributes : true,
    ignoreNameSpace : false,
    parseNodeValue : false,
    tagValueProcessor : a =&gt; {
      if(Array.isArray(a)){
        return a.join(',');
      }
      return a;
    }
  };
  
  const flatten = (data) =&gt; {
    return data.map(row =&gt; {
      const fieldNames = Object.keys(row);
      for (const fieldName of fieldNames) {
        if(Array.isArray(row[fieldName])){
          row[fieldName] = row[fieldName].join(',');
        }
  
        if(typeof row[fieldName] === 'object'){
          row[fieldName] = JSON.stringify(row[fieldName]);
        }
      }
      return row;
    });
  };
  
  
  function findTheArray(o) {
    if(Array.isArray(o)){
      return o;
    }
    var result, p; 
    for (p in o) {
        if( o.hasOwnProperty(p) &amp;&amp; typeof o[p] === 'object' ) {
            result = findTheArray(o[p]);
            if(result){
                return result;
            }
        }
    }
    return result;
  }
  
  
  module.exports = function parseData(data) {
    return new Promise((resolve, reject) =&gt; {
      try {
        const isValid = parse.validate(data);
        if (isValid === true) {
          const pData = parse.parse(data, options);
          const array = findTheArray(pData);
          if(array){
            resolve(flatten(array));
          } else {
            reject('Can\'t find any goodies!');
          }
        } else {
          reject(isValid.err);
        }
      } catch (err) {
        reject(err);
      }
    });
  };
  </code></pre>
  
  <p>I've worked on the this area of the code but haven't been able to get any success:</p>
  
  <pre><code>if(typeof row[fieldName] === 'object'){
          row[fieldName] = JSON.stringify(row[fieldName])
  </code></pre>
  
  <p>Ideas?
  thanks</p>