Skip to main content
17-Peridot
September 5, 2022
Solved

Converting Json to XML usin JS service in thingworx

  • September 5, 2022
  • 1 reply
  • 2685 views

I am trying to convert json code to xml code. I am getting errors for the following code. Help me to fix it. 


js code:

try{
let JSON = "{College:{entry: [{ Student : 'shiv', Roll_No: 12},{ Student : 'yadav',Roll_No: 56}]}}";
let result = OBJtoXML(JSON);

function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(obj[prop][array]));
xml += "</" + prop + ">";
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
}
xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml;
}
}catch(e){
logger.error("jsCodeToConvertJsonToXml: error is "+e+" in linenumber "+e.lineNumber);
}

 

errors:

1. error is TypeError: OBJtoXML is not a function, it is undefined. in linenumber 3.

2.  Message ::Unable To Convert From org.mozilla.javascript.UniqueTag to XML

Best answer by nmutter

I numbered the issues your code had

try {
	// 1. cannot name variable JSON - as JSON object already exists
	// 2. json string must be a valid json object, having "" around attribute names
	let json = '{"College":{"entry": [{"Student": "shiv", "Roll_No": 12},{"Student": "yadav","Roll_No": 56}]}}';
	// 3. don't use let for result variable when used in a block (here try,catch). the variable will only exist in the block
	// 4. when you pass only a string, it will not be interpreted as an object. need to parse the string first
 result = OBJtoXML(JSON.parse(json));
} catch (e) {
	logger.error("jsCodeToConvertJsonToXml: error is " + e + " in linenumber " + e.lineNumber);
	throw e;
}

function OBJtoXML(obj) {
	var xml = '';
	for (var prop in obj) {
		xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
		if (obj[prop] instanceof Array) {
			for (var array in obj[prop]) {
				xml += "<" + prop + ">";
				xml += OBJtoXML(new Object(obj[prop][array]));
				xml += "</" + prop + ">";
			}
		} else if (typeof obj[prop] == "object") {
			xml += OBJtoXML(new Object(obj[prop]));
		} else {
			xml += obj[prop];
		}
		xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
	}
	xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
	return xml;
}

Returns:

<College><entry><Student>shiv</Student><Roll_No>12</Roll_No></entry><entry><Student>yadav</Student><Roll_No>56</Roll_No></entry></College>

 

Also, next time please use the

nmutter_0-1662408662781.png

feature for source code.

1 reply

nmutter16-PearlAnswer
16-Pearl
September 5, 2022

I numbered the issues your code had

try {
	// 1. cannot name variable JSON - as JSON object already exists
	// 2. json string must be a valid json object, having "" around attribute names
	let json = '{"College":{"entry": [{"Student": "shiv", "Roll_No": 12},{"Student": "yadav","Roll_No": 56}]}}';
	// 3. don't use let for result variable when used in a block (here try,catch). the variable will only exist in the block
	// 4. when you pass only a string, it will not be interpreted as an object. need to parse the string first
 result = OBJtoXML(JSON.parse(json));
} catch (e) {
	logger.error("jsCodeToConvertJsonToXml: error is " + e + " in linenumber " + e.lineNumber);
	throw e;
}

function OBJtoXML(obj) {
	var xml = '';
	for (var prop in obj) {
		xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
		if (obj[prop] instanceof Array) {
			for (var array in obj[prop]) {
				xml += "<" + prop + ">";
				xml += OBJtoXML(new Object(obj[prop][array]));
				xml += "</" + prop + ">";
			}
		} else if (typeof obj[prop] == "object") {
			xml += OBJtoXML(new Object(obj[prop]));
		} else {
			xml += obj[prop];
		}
		xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
	}
	xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
	return xml;
}

Returns:

<College><entry><Student>shiv</Student><Roll_No>12</Roll_No></entry><entry><Student>yadav</Student><Roll_No>56</Roll_No></entry></College>

 

Also, next time please use the

nmutter_0-1662408662781.png

feature for source code.

17-Peridot
September 5, 2022

Thanks @nmutter . If i passed a long json code, this js is not working. Kindly pls check the code with this json input. It is not working. Output is null. And there is no errors. How to fix this

let json = '{"BookID":"DTESTN01","IssueDate":"5 / 2021","PublicationInfo":{"PartsBookPartCode":[{"Language":"EN","PartNumber":"7DTESTN01E","Title":"1835RECTANGULARBALER"},{"Language":"TT","PartNumber":"7DTESTN01E","Title":"1835RECTANGULARBALER"},{"Language":"NA","PartNumber":"7DTESTN01N","Title":"1835RECTANGULARBALER"},{"Language":"PL","PartNumber":"7DTESTN01PL","Title":"1835PRASA-MAŁAKOSTKA"}],"PbatBuildNr":5122},"Suffix":"A"}';

AP_9587236_0-1662418174639.png

 

16-Pearl
September 6, 2022

the code does not convert correctly, if there is more than one element in the root.

If you do it like this it works:

result = OBJtoXML(JSON.parse('{"wrapper":' + json + '}'));

 

 You may want to extend the code to support this. but xml will always need to have one root element it can wrap everything in it.