mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-26 09:46:17 -04:00
feat: add jsonata query operation
This commit is contained in:
parent
1b8c229df2
commit
5b15e75479
3 changed files with 75 additions and 0 deletions
65
src/core/operations/Jsonata.mjs
Normal file
65
src/core/operations/Jsonata.mjs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* @author Jon K (jon@ajarsoftware.com)
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import jsonata from "jsonata";
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* Jsonata Query operation
|
||||
*/
|
||||
class JsonataQuery extends Operation {
|
||||
/**
|
||||
* JsonataQuery constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Jsonata Query";
|
||||
this.module = "Code";
|
||||
this.description =
|
||||
"Query and transform JSON data with a jsonata query.";
|
||||
this.infoURL = "https://docs.jsonata.org/overview.html";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
name: "Query",
|
||||
type: "text",
|
||||
value: "string",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {JSON}
|
||||
*/
|
||||
async run(input, args) {
|
||||
const [query] = args;
|
||||
let result, jsonObj;
|
||||
|
||||
try {
|
||||
jsonObj = JSON.parse(input);
|
||||
} catch (err) {
|
||||
throw new OperationError(`Invalid input JSON: ${err.message}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const expression = jsonata(query);
|
||||
result = await expression.evaluate(jsonObj);
|
||||
} catch (err) {
|
||||
throw new OperationError(
|
||||
`Invalid Jsonata Expression: ${err.message}`
|
||||
);
|
||||
}
|
||||
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
}
|
||||
|
||||
export default JsonataQuery;
|
Loading…
Add table
Add a link
Reference in a new issue