How to convert flat json object into hierarchical xml?

I have json object like this:

 var client = {
   "id": 1,
   "name": "John Doe",
   "name2": "Jane Doe",
   "email": "John@test.com",
   "phone": "+1-306-5555555",
   "phoneType": "Home",
   "phone2": "+1-306-5556666",
   "phone2Type": "Business",
   "gender": "Male",
   "address": "1000 Center AVE",
   "postCode": "S7J 1P3",
   "city": "Saskatoon",
   "province": "SK",
   "country": "CA",
   "dob": "1990-12-11T02:00:00.000Z",
   "maritalStatus": "Single",
   "occupation": "Software Developer",
   "createdAt": "2015-12-07T08:14:21.000Z",
   "updatedAt": "2016-01-19T13:35:10.000Z"
 };

Which I want to convert into XML, this isn’t a problem I know there great library like x2js that can do that for me.

My problem is xml file, need to follow a standard like this:

   <GeneralPartyInfo>
<NameInfo>
    <PersonName>
        <Surname>Doe </Surname>
        <GivenName>John</GivenName>
    </PersonName>
    <SupplementaryNameInfo>
        <SupplementaryName>Jane Doe</SupplementaryName>
    </SupplementaryNameInfo>
</NameInfo>
<Addr>
    <DetailAddr>
        <StreetName>Centre</StreetName>
        <StreetNumber>1000</StreetNumber>
        <csio:StreetTypeCd>AVE</csio:StreetTypeCd>
    </DetailAddr>
    <City>Saskatoon</City>
    <StateProvCd>SK</StateProvCd>
    <PostalCode>S7J 1P3</PostalCode>
    <CountryCd>CA</CountryCd>
</Addr>
<Communications>
    <PhoneInfo>
        <PhoneTypeCd>Phone</PhoneTypeCd>
        <CommunicationUseCd>Home</CommunicationUseCd>
        <PhoneNumber>+1-306-5555555</PhoneNumber>
    </PhoneInfo>
    <PhoneInfo>
        <PhoneTypeCd>Phone</PhoneTypeCd>
        <CommunicationUseCd>Business</CommunicationUseCd>
        <PhoneNumber>+1-306-5556666</PhoneNumber>
    </PhoneInfo>
</Communications>

you can xml is nested while my json isn’t, not mention name need to spliced to firstName and lastName and be inside PersonName which is child of NameInfo, and GeneralPartyInfo be the root parent (same thing for the address).

there two problems I have, the first one is a missing tags for example firstname is inside PersonName which inside NameInfo which inside GeneralPartyInfo and I can’t do this

  var firstName = client.name.split(' ').slice(0, -1).join(' ');
  var lastName =  client.name.split(' ').slice(-1).join(' ');
  delete client.name;

client.PersonName.push({
        GiveName: firstName,
        Surname: lastName,
});

because JSON is an object not an array, it requires for me to convert JSON into an array (which is not recommended by other professionals here in SO), or I can save each one (Name, address, communication) into it separate json object then concat all of them together abdthe only way I found in SO to add parent json object was like this

  var obj = {fistName: client.name.split(' ').slice(0, -1).join(' '), lastName: client.name.split(' ').slice(-1).join(' ');};
     var obj2 = { "PersonName" : obj };
     var obj3 = { "NameInfo" : obj2 };

my second problem is find a way to replace keys for example I want to replace postcode to PostalCode or phoneType to CommunicationUseCd

or just do it this way

    var xml =  "<GeneralPartyInfo><NameInfo><PersonName>
   <Surname>" + client.name.split(' ').slice(0, -1).join(' '); + "</Surname>
   <GivenName>"+client.name.split(' ').slice(-1).join(' ')+"</GivenName>"

but this solution is tedious, not efficient and doesn’t look pretty and I’m looking more for stability and cleanliness of code.

1 Like

It would be a lot easier if you have the JSON and XML structures actually match instead of defining completely different structures for each.

You don’t even have the names of each field matching between the two.

Agreed. Trr getting your JSON to be like this

{ "GeneralPartyInfo" : {
  "NameInfo" : {
    "PersonName" : 
      { "Surname" : "Doe",
        "GivenName" : "John" },
    "SupplementaryNameInfo" : 
      { "SupplementaryName" : "Jane Doe" }
  },
  "Addr" : { 
    "DetailAddr" : 
      { "StreetName" : "Centre",
        "StreetNumber" : "1000",
        "csio:StreetTypeCd" : "AVE" },
    "City" : "Saskatoon",
    "StateProvCd" : "SK",
    "PostalCode" : "S7J 1P3",
    "CountryCd" : "CA"
  },
  "Communications" : {
    "PhoneInfo" : 
      { "PhoneTypeCd" : "Phone",
        "CommunicationUseCd" : "Home",
        "PhoneNumber" : "+1-306-5555555" },
    "PhoneInfo" : 
      { "PhoneTypeCd" : "Phone",
        "CommunicationUseCd" : "Business",
        "PhoneNumber" : "+1-306-5556666" }
    }
  }
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.