Search code examples
node.jsjsonnode-modulesnodejs-streamnodejs-server

Restructure json based on actual and expected data


I want to restructure json based on original json data and the expected json data.

If you look closely in the original json data I have country outside of the Male/Female attributes. I would want the country module to be inside the Male/ Female attribute based on the orientation attribute inside the country module. So in the afterdata I would have 1 country module in Male attribute(since there is 1 male record) and 2 country module in Female attribute(since there are 2 female records).

Original json data looks like this:

{
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ]
}

Expected json data:

{ 
  "Implementations":    [
    {      
      "Male": {
        "Gender": "Male"         
         "Country": [
                {
                  "Orientation": "Male",          
                  "Name": ABCD
                }
            ],
              "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]

      },
      "Female": {
        "Gender": "Female"          
        "Country": [
                {
                  "Orientation": "Female",
                  "Name": EFGH
                },
                {
                  "Orientation": "Female",
                  "Name": IJKL        
                }
              ],
        "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]
        }
    }
  ]

}

Program:

 var Implementations = {  
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ]
}

var output = [];
for (k in Implementations.Implementations.Male) {
  var temp = [];  
  for (j in Implementations.Implementations.Male[k]) {
    temp.push({
      Country: j      
    });
  }
  output.push({
    "Implementations": k,
    Country: temp
  });
}
console.log(output);

Thank you in advance!


Solution

  • Your program does not work because Implementations.Implementations is an array, it doesn't have field named Male.

    Here is a working code snippet:

    //Original JSON data in question.
    var Implementations = {  
      "Implementations": [
        {      
          "Male": {
            "Gender": "Male"              
          },
          "Female": {
            "Gender": "Female"       
          },
    
          "Country": [
            {
              "Orientation": "Male",          
              "Name": ABCD
            },
            {
              "Orientation": "Female",
              "Name": EFGH
            },
            {
              "Orientation": "Female",
              "Name": IJKL        
            }
          ],
          "State": [
            {          
              "Address": "XYZ Street",
              "ZipCode": "US"
            }
          ]
        }
      ]
    }
    
    // Program that make the conversion
    var finalResult = [];
    for (var i=0; i<Implementations.Implementations.length; i++) {
      var currentImplementation = Implementations.Implementations[i];
      var targetObj = {
        "Male": {
          "Gender": "Male",
          "Country": [],
          "State": currentImplementation.State
        },
        "Female": {
          "Gender": "Female",
          "Country": [],
          "State": currentImplementation.State
        }
      };
      for (var j=0; j<currentImplementation.Country.length; j++) {
        var currentCountry = currentImplementation.Country[j];
        if (currentCountry.Orientation === 'Male') {
          targetObj.Male.Country.push(currentCountry);
        } else if (currentCountry.Orientation === 'Female') {
          targetObj.Female.Country.push(currentCountry);
        }
      }
      finalResult.push(targetObj);
    }
    
    console.log(JSON.stringify(finalResult));