0

I am getting a json data having structure

{
    SearchDAO: [
        {
            PERSONADDRESS_H_ADDRESS_LINE_ONE: "599 Waterloo place",
            PERSON_H_BIRTHDATE_VALUE: "1939-01-11 00:00:00",
            PERSON_H_CREATE_TS: "2012-11-22 11:17:13.879",
            PERSON_H_GENDER_CD: "M"
        }
    ]
}

As you can see in the data set two type of keys are there 1. starting with "PERSONADDRESS" 2. starting with "PERSON"

I have to convert this structure to

{
    "PERSON":[
         {
             H_BIRTHDATE_VALUE: "1939-01-11 00:00:00",
             H_CREATE_TS: "2012-11-22 11:17:13.879",
             H_GENDER_CD: "M"
         }
    ],
    "PERSONADDRESS":[
         {
             H_ADDRESS_LINE_ONE: "599 Waterloo place"
         }
    ]

I am struggling to do this. As It need to splice key string and change the structure

Please help

I am trying something like this

 $.each(data.SearchDAO[0], function(k, v) {
                               var streetaddress= k.substr(0, k.indexOf('_'));
                               console.log(streetaddress)
                               if(returnVar[streetaddress] == undefined){
                                   thisItem = [];
                                   returnVar[streetaddress]  = thisItem;
                               }
                               else {
                                   thisItem = returnVar[streetaddress];
                               }
                               var obj = {};
                               obj.issueValue = v;
                               thisItem.push(obj);
                           });
 console.log(thisItem)
James
  • 2,195
  • 1
  • 19
  • 22
Saurabh Sinha
  • 1,772
  • 4
  • 27
  • 54

2 Answers2

0

I solved the problem

here is my code

   returnVar={};

$.each(data.SearchDAO[0], function(k, v) {
                               var streetaddress= k.substr(0, k.indexOf('_'));
                               var keyFinal= k.substr(k.indexOf('_')+1,k.length-1);
                               console.log(keyFinal)
                               if(returnVar[streetaddress] == undefined){
                                    thisItem = {};
                                    returnVar[streetaddress]  = thisItem;
                                    thisItem[keyFinal]=v;
                                }
                               else {
                                    thisItem = returnVar[streetaddress];
                                    thisItem[keyFinal]=v;
                                }

                            });
                          console.log(returnVar)
Saurabh Sinha
  • 1,772
  • 4
  • 27
  • 54
0

This should do it (for multiple persons as well):

var json = {
    SearchDAO: [
        {
            PERSONADDRESS_H_ADDRESS_LINE_ONE: "599 Waterloo place",
            PERSON_H_BIRTHDATE_VALUE: "1939-01-11 00:00:00",
            PERSON_H_CREATE_TS: "2012-11-22 11:17:13.879",
            PERSON_H_GENDER_CD: "M"
        },
        {
            PERSONADDRESS_H_ADDRESS_LINE_ONE: "123 Place",
            PERSON_H_BIRTHDATE_VALUE: "1901-01-01 00:00:00",
            PERSON_H_CREATE_TS: "2001-01-01 00:00:00.000",
            PERSON_H_GENDER_CD: "F"
        }
    ]
}

var converted = {};
for (var i = 0; i < json.SearchDAO.length; i++)
{
    var row = json.SearchDAO[i];
    var keys = Object.keys(row);
    for (var j = 0; j < keys.length; j++)
    {
        var key = keys[j];
        var key_prefix = key.substr(0, key.indexOf('_'));
        var key_suffix = key.substr(key.indexOf('_') + 1);

        if (!(key_prefix in converted))    converted[key_prefix] = [];
        if (!(i in converted[key_prefix])) converted[key_prefix][i] = {};

        converted[key_prefix][i][key_suffix] = row[key_prefix + "_" + key_suffix];
    }
}
Rob Lokhorst
  • 169
  • 4