Skip to content

Commit 3c5eea5

Browse files
authored
Merge pull request #1587 from jonking-ajar/feat/jsonata-operation
2 parents 497e2a9 + 91125f0 commit 3c5eea5

File tree

6 files changed

+631
-6
lines changed

6 files changed

+631
-6
lines changed

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"js-sha3": "^0.9.3",
141141
"jsesc": "^3.0.2",
142142
"json5": "^2.2.3",
143+
"jsonata": "^2.0.3",
143144
"jsonpath-plus": "^9.0.0",
144145
"jsonwebtoken": "8.5.1",
145146
"jsqr": "^1.4.0",

src/core/config/Categories.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
"Regular expression",
370370
"XPath expression",
371371
"JPath expression",
372+
"Jsonata Query",
372373
"CSS selector",
373374
"Extract EXIF",
374375
"Extract ID3",

src/core/operations/Jsonata.mjs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @author Jon K ([email protected])
3+
* @copyright Crown Copyright 2016
4+
* @license Apache-2.0
5+
*/
6+
7+
import jsonata from "jsonata";
8+
import Operation from "../Operation.mjs";
9+
import OperationError from "../errors/OperationError.mjs";
10+
11+
/**
12+
* Jsonata Query operation
13+
*/
14+
class JsonataQuery extends Operation {
15+
/**
16+
* JsonataQuery constructor
17+
*/
18+
constructor() {
19+
super();
20+
21+
this.name = "Jsonata Query";
22+
this.module = "Code";
23+
this.description =
24+
"Query and transform JSON data with a jsonata query.";
25+
this.infoURL = "https://docs.jsonata.org/overview.html";
26+
this.inputType = "string";
27+
this.outputType = "string";
28+
this.args = [
29+
{
30+
name: "Query",
31+
type: "text",
32+
value: "string",
33+
},
34+
];
35+
}
36+
37+
/**
38+
* @param {string} input
39+
* @param {Object[]} args
40+
* @returns {string}
41+
*/
42+
async run(input, args) {
43+
const [query] = args;
44+
let result, jsonObj;
45+
46+
try {
47+
jsonObj = JSON.parse(input);
48+
} catch (err) {
49+
throw new OperationError(`Invalid input JSON: ${err.message}`);
50+
}
51+
52+
try {
53+
const expression = jsonata(query);
54+
result = await expression.evaluate(jsonObj);
55+
} catch (err) {
56+
throw new OperationError(
57+
`Invalid Jsonata Expression: ${err.message}`
58+
);
59+
}
60+
61+
return JSON.stringify(result === undefined ? "" : result);
62+
}
63+
}
64+
65+
export default JsonataQuery;

tests/operations/index.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
* @license Apache-2.0
1212
*/
1313

14-
import {
15-
setLongTestFailure,
16-
logTestReport,
17-
} from "../lib/utils.mjs";
14+
import { setLongTestFailure, logTestReport } from "../lib/utils.mjs";
1815

1916
import TestRegister from "../lib/TestRegister.mjs";
2017
import "./tests/AESKeyWrap.mjs";
@@ -89,6 +86,7 @@ import "./tests/IndexOfCoincidence.mjs";
8986
import "./tests/JA3Fingerprint.mjs";
9087
import "./tests/JA4.mjs";
9188
import "./tests/JA3SFingerprint.mjs";
89+
import "./tests/Jsonata.mjs";
9290
import "./tests/JSONBeautify.mjs";
9391
import "./tests/JSONMinify.mjs";
9492
import "./tests/JSONtoCSV.mjs";
@@ -181,14 +179,14 @@ const testStatus = {
181179
allTestsPassing: true,
182180
counts: {
183181
total: 0,
184-
}
182+
},
185183
};
186184

187185
setLongTestFailure();
188186

189187
const logOpsTestReport = logTestReport.bind(null, testStatus);
190188

191-
(async function() {
189+
(async function () {
192190
const results = await TestRegister.runTests();
193191
logOpsTestReport(results);
194192
})();

0 commit comments

Comments
 (0)