javascript - convert flat json object into hierarchical xml -
i have json object 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 want convert xml, isn't problem know there great library x2js can me.
my problem xml file, need follow standard 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 nested while json isn't, not mention name need spliced firstname , lastname , inside personname child of nameinfo, , generalpartyinfo root parent (same thing address).
there 2 problems have, first 1 missing tags example firstname inside personname inside nameinfo inside generalpartyinfo , can't 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 object not array, requires me convert json array (which not recommended other professionals here in so), or can save each 1 (name, address, communication) separate json object concat of them abdthe way found in add parent json object 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 find way replace keys example want replace postcode postalcode or phonetype communicationusecd
or way
var xml = "<generalpartyinfo><nameinfo><personname> <surname>" + client.name.split(' ').slice(0, -1).join(' '); + "</surname> <givenname>"+client.name.split(' ').slice(-1).join(' ')+"</givenname>"
but solution tedious, not efficient , doesn't pretty , i'm looking more stability , cleanliness of code.
for example how can manipulate json object add parent selected elements can replace keys through list have keys replaced example.
var translationobj = { "firstname": "givenname", "lastname": "personname", }
ps: have lodash/underscore installed solution using underscore great.
Comments
Post a Comment