VCF Automation Blog

from Stefan Schnell

Flatten Complex Objects


/**
 *
 * @module de.stschnell
 *
 * @version 1.0.0
 *
 * @param {Properties} in_objectToFlatten Object which should be flattened
 *
 * @outputType Properties
 *
 * @description Flattens complex objects
 */
function flattenObject(in_objectToFlatten) {

// Begin ---------------------------------------------------------------

/**
 * Flattens complex objects
 *
 * Original code from Muthu Krishnan
 * https://stackoverflow.com/questions/44134212
 * with modifications and additions
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @param {Properties} in_objectToFlatten - Object to flatten
 * @returns {Properties} flatObject - Flattens object
 */
var _flattenObject = {
  Main : function(objectToFlatten) {
    var flatObject = {};
    for (var key in objectToFlatten) {
      if (
        (typeof objectToFlatten[key]) === "object" &&
        objectToFlatten[key] !== null
      ) {
        if (objectToFlatten[key] instanceof Date) {
          flatObject[key] = new Date(objectToFlatten[key]);
        } else {
          var nestedObject = _flattenObject.Main(objectToFlatten[key]);
          if (Object.keys(nestedObject).length > 0) {
            for (var flatKey in nestedObject) {
              // Hint: Not every character works here
              flatObject[key + '$' + flatKey] = nestedObject[flatKey];
            }
          } else {
            flatObject[key] = objectToFlatten[key];
          }
        }
      } else {
        flatObject[key] = objectToFlatten[key];
      }
    }
    return flatObject;
  }
};

// Main
return _flattenObject.Main(in_objectToFlatten);

// End -----------------------------------------------------------------

}

/**
 *
 * @module de.stschnell
 *
 * @version 1.0.0
 *
 * @outputType string
 *
 * @description Example for flattenObject
 */
function flattenObjectExample() {

// Begin ---------------------------------------------------------------

/**
 * Example to use flattenObject
 * 
 * @author Stefan Schnell <mail@stefan-schnell.de>
 */

var obj = {
  a : 1,
  b : {
    ba : 2,
  },
  c : [1, 2, 3],
  d : [
    {
      da : ['da1', 'da2', 'da3'],
    },
    {
      db : ['db1', 'db2', 'db3'],
    },
    {
      dc : [
        {
          dca : ['dca1', 'dca2', null],
        },
      ],
    },
    null,
    undefined,
    NaN,
    0,
    '',
    ['a', 'b', ['c', 'd'], ['e', 'f']],
    [
      {
        dda : 1,
      },
      {
        ddb : 1,
      },
    ],
  ],
  e : [true, false],
  f : [[1, 2]],
  g : function() {
    return "Hello World";
  },
  h : new Date(),
  i : [],
  j : {},
  z : "End"
};

// Flatten object
var flatObj = System.getModule("de.stschnell").flattenObject(obj);

// Output elements of the flat object
Object.keys(flatObj).forEach( function(propertyName) {
  System.log(propertyName + " : " + flatObj[propertyName]);
});

/* result =>
  a : 1
  b$ba : 2
  c$0 : 1
  c$1 : 2
  c$2 : 3
  d$0$da$0 : da1
  d$0$da$1 : da2
  d$0$da$2 : da3
  d$1$db$0 : db1
  d$1$db$1 : db2
  d$1$db$2 : db3
  d$2$dc$0$dca$0 : dca1
  d$2$dc$0$dca$1 : dca2
  d$2$dc$0$dca$2 : null
  d$3 : null
  d$4 : undefined
  d$5 : NaN
  d$6 : 0
  d$7 : 
  d$8$0 : a
  d$8$1 : b
  d$8$2$0 : c
  d$8$2$1 : d
  d$8$3$0 : e
  d$8$3$1 : f
  d$9$0$dda : 1
  d$9$1$ddb : 1
  e$0 : true
  e$1 : false
  f$0$0 : 1
  f$0$1 : 2
  g : 
  function () {
      return "Hello World";
  }
  h : Wed May 24 2023 09:25:21 GMT-0000 (GMT)
  i : 
  j : [object Object]
  z : End
*/

// End -----------------------------------------------------------------

}