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": "",
   "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:

        <Surname>Doe </Surname>
        <SupplementaryName>Jane Doe</SupplementaryName>
    <PostalCode>S7J 1P3</PostalCode>

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 =' ').slice(0, -1).join(' ');
  var lastName =' ').slice(-1).join(' ');

        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:' ').slice(0, -1).join(' '), lastName:' ').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>" +' ').slice(0, -1).join(' '); + "</Surname>
   <GivenName>"' ').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.