mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-09 15:55:01 -04:00
Merge pull request #15 from jpledref/feat-issue-1607-add-xml-to-json
feat: add XML to JSON operation
This commit is contained in:
commit
acf62c634d
5 changed files with 86 additions and 0 deletions
|
@ -66,6 +66,7 @@
|
|||
"CSV to JSON",
|
||||
"JSON to CSV",
|
||||
"Avro to JSON",
|
||||
"XML to JSON",
|
||||
"CBOR Encode",
|
||||
"CBOR Decode"
|
||||
]
|
||||
|
|
49
src/core/operations/XMLToJSON.mjs
Normal file
49
src/core/operations/XMLToJSON.mjs
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* @author jpledref [jp.ledref@gmail.com]
|
||||
* @copyright Crown Copyright 2023
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import xml2js from "xml2js";
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* XML to JSON operation
|
||||
*/
|
||||
class XMLToJSON extends Operation {
|
||||
|
||||
/**
|
||||
* XMLToJSON constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "XML to JSON";
|
||||
this.module = "Default";
|
||||
this.description = "Converts XML data to JSON format.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/XML";
|
||||
this.inputType = "string";
|
||||
this.outputType = "JSON";
|
||||
this.args = [
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {JSON}
|
||||
*/
|
||||
run(input, args) {
|
||||
let result;
|
||||
try {
|
||||
xml2js.parseString(input, {}, (e, r) => result = JSON.stringify(r));
|
||||
return JSON.parse(result);
|
||||
} catch (err) {
|
||||
throw new OperationError("Unable to parse XML to JSON: " + err.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default XMLToJSON;
|
Loading…
Add table
Add a link
Reference in a new issue