0

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!

Eva
  • 109
  • 9

1 Answers1

1

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));
shaochuancs
  • 15,342
  • 3
  • 54
  • 62
  • Thank you and appreciate your quick response. Also, will using node-Json-transform be an easier way to restructure? I haven’t tried it yet, let me know your suggestions. – Eva Jul 09 '19 at 16:26
  • I think using Node would be easier to transform JSON, but anyway that’s opinion based. Also, if this answer helps and fixed the problem, would you kindly mark it as valid? – shaochuancs Jul 09 '19 at 21:58
  • Ah, you’re asking Node module “node-json-transform”. I think this is a pretty easy convert, and not necessary to introduce a new module. But if you have a lot of similar convert work, maybe using a module is a good idea. – shaochuancs Jul 09 '19 at 22:09
  • How do I rename a property key? In the example above, Country, State are property keys. How do I rename State to Statecount? – Eva Jul 10 '19 at 12:47