Parse XML with OK JSON
Build a script to parse XML and view the structure in OK JSON.
Getting Started
Step 1
Download fast-xml-parser
(opens in a new tab) and put the file in packages
folder.
You can also grab the file at fast-xml-parser - Libraries - cdnjs (opens in a new tab). Just download the js file fxparser.min.js
.
Step 2
Create a script file in the script folder with the following code.
parse-xml.js
/*
@okjson.schemaVersion 1
@okjson.name Parse XML
@okjson.description Parse XML, convert to JSON, and view in OK JSON.
@okjson.icon chevron.left.forwardslash.chevron.right
@okjson.preprocessInput no
@okjson.openResultInNewWindow no
@okjson.author OK JSON
@okjson.authorURL https://okjson.app
*/
$include('fxparser.min');
function main(string) {
const parser = new XMLParser();
const result = parser.parse(string);
const json = JSON.stringify(result, null, 4);
$utility.viewInOKJSON(json);
return '';
}
Step 3
Run the script and XML string as the input.