XML to JSON and vice-versa

Develop

  1. Create a new project or use an existing one.
  2. Select the ScriptingServices sub-folder of the project and open the pop-up menu.
  3. Choose New -> Scripting Service.
  4. From the list of available templates choose Server-Side JavaScript Service.
  5. Give it a meaningful name (e.g xml_usage.js).
  6. Replace the generated code in xml_usage.js with the following one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	/* globals $ */
	/* eslint-env node, dirigible */
	
	var xml = require('utils/xml');
	var response = require('net/http/response');
	
	var jsonInput = {
		'firstName': 'John',
		'lastName': 'Doe',
		'bio': {
			'age': 24,
			'sex': 'male'
		}
	};
	
	var xmlInput = 
		"<firstName>John</firstName>" +
		"<lastName>Doe</lastName>" + 
		"<bio>" + 
		"<age>24</age>" +
		"<sex>male</sex>" +
		"</bio>";
	
	response.println(xml.fromJson(jsonInput));
	response.println(xml.toJson(xmlInput));
	
	response.flush();
	response.close();
  1. Select the Preview tab.
  2. Click on xml_usage.js from the Workspace Explorer and check the raw result:
1
2
	<firstName>John</firstName><lastName>Doe</lastName><bio><sex>male</sex><age>24</age></bio>
	{"firstName":"John","lastName":"Doe","bio":{"sex":"male","age":24}}

toJson(xmlString) - converts XML content to JSON

fromJson(jsonString) - converts JSON content to XML

Discover

To discover all available services, you can go to the Registry.

  1. From the main menu, choose Window -> Show Perspective -> Registry.
  2. The Registry perspective represents a view to the enabled runtime content. From its menu, choose Discover -> JavaScript to open the currently available server-side JavaScript service endpoints.
  3. You can see the list of available endpoints, where you can find yours by naming convention: {project}/{service path}
Edit