File tree Expand file tree Collapse file tree 6 files changed +631
-6
lines changed
Expand file tree Collapse file tree 6 files changed +631
-6
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 369369 " Regular expression" ,
370370 " XPath expression" ,
371371 " JPath expression" ,
372+ " Jsonata Query" ,
372373 " CSS selector" ,
373374 " Extract EXIF" ,
374375 " Extract ID3" ,
Original file line number Diff line number Diff line change 1+ /**
2+ * @author Jon K (jon@ajarsoftware.com)
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 ;
Original file line number Diff line number Diff line change 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
1916import TestRegister from "../lib/TestRegister.mjs" ;
2017import "./tests/AESKeyWrap.mjs" ;
@@ -89,6 +86,7 @@ import "./tests/IndexOfCoincidence.mjs";
8986import "./tests/JA3Fingerprint.mjs" ;
9087import "./tests/JA4.mjs" ;
9188import "./tests/JA3SFingerprint.mjs" ;
89+ import "./tests/Jsonata.mjs" ;
9290import "./tests/JSONBeautify.mjs" ;
9391import "./tests/JSONMinify.mjs" ;
9492import "./tests/JSONtoCSV.mjs" ;
@@ -181,14 +179,14 @@ const testStatus = {
181179 allTestsPassing : true ,
182180 counts : {
183181 total : 0 ,
184- }
182+ } ,
185183} ;
186184
187185setLongTestFailure ( ) ;
188186
189187const logOpsTestReport = logTestReport . bind ( null , testStatus ) ;
190188
191- ( async function ( ) {
189+ ( async function ( ) {
192190 const results = await TestRegister . runTests ( ) ;
193191 logOpsTestReport ( results ) ;
194192} ) ( ) ;
You can’t perform that action at this time.
0 commit comments