Skip to content Skip to sidebar Skip to footer

Javascript: Change Json Data Array Into New Format

I am trying to work with a json data which needs to be changed in many ways. My current json data is following: { 'file1': { 'function1': { 'calls': { '105:4':

Solution 1:

Assuming the structure of your input is consistenly defined as shown in your question (ie, that no "saftey checks" are needed, etc), then you could approach this using a combination of Object.entries(), Array.map() and spread syntax as shown below.

See inline documentation in this code snippet for details on how to achieve that:

functiontransformData(data, programName) {

  /* Define local parse helper to extract number from NUMBER:STRING format */constparseHelper = (str) => Number.parseInt(str.split(':')[0]);
      
  /* Define local parse helper to extract group number from STRINGNUMBER 
  format */constparseGroup = (str) => Number.parseInt(str.replace(/^[a-z]+/,""))
      
  /* Create a root object with specified program name */return {
    name : programName,
    
    /* Iterate each file name and object entry of input */
    children : Object.entries(input).map(([fileName, fileObject]) => {

      /* Iterate function name and object of current file object */const fileChildren = Object.entries(fileObject)
        .map(([functionName, functionObject]) => {

        /* Iterate function name and object of current file object */const lines = Object.entries(functionObject)
          .reduce((target, [functionKey, functionValue]) => {

            if(functionKey === "calls") {

              /* If function key is calls, interpret this value as data to be
              transformed to desired calls object shape */const calls = Object.entries(functionValue)
                .map(([callKey, callObject]) => {

                return {
                  line : parseHelper(callKey),
                  file : callObject['file'],
                  function : callObject['function']
                }
              });
              
              /* Inject calls object into lines result */return {
                ...target,
                calls
              };
            }
            else {

              /* Otherwise, interpret this value as data to be transformed to 
                 desired lines object shape */const lineValues = Object.entries(functionValue)
                .map(([key, value]) => {

                /* If value is an array, map key/value pair to a nested array
                   in resulting linesValues array */returnArray.isArray(value) ? [key, ...value]
                 .map(parseHelper) : parseHelper(value)
              })

              /* Inject line values into function key of result */return {
                ...target,
                [functionKey] : lineValues
              }
            }

        }, {});
        
        /* Inject lines into function result */return {
          name : functionName,
          ...lines,
          group : parseGroup(functionName)
        }
      });

      /* Map file object to name/children pairing */return { 
        name : fileName,
        children : fileChildren,
          group : parseGroup(fileName)
      }
    }),
    
    group : 0
  }
}

const input = {
  "file1": {
    "function1": {
      "calls": {
        "105:4": {
          "file": "file2",
          "function": "function5"
        },
        "106:4": {
          "file": "file2",
          "function": "function6"
        }
      },
      "lines1": {
        "123": "102:0",
        "456": "105:8"
      },
      "lines2": {
        "102:0": [
          "102:0"
        ],
        "105:4": [
          "106:4",
          "107:1"
        ],
        "106:4": [
          "107:1"
        ]
      }
    }
  }
};

console.log(transformData(input, "program"))

Hope that helps!

Post a Comment for "Javascript: Change Json Data Array Into New Format"