From de79a42ec77bfc2a8b42505e2764dc7fddd453c6 Mon Sep 17 00:00:00 2001 From: Archangelza1 Date: Tue, 15 Oct 2024 13:06:36 +0200 Subject: [PATCH] feat: add option to return multiple matches for JSON capture (#3311) --- packages/commons/engine_util.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/commons/engine_util.js b/packages/commons/engine_util.js index 8e5dc732ad..57067ac91b 100644 --- a/packages/commons/engine_util.js +++ b/packages/commons/engine_util.js @@ -271,8 +271,7 @@ function templateObjectOrArray(o, context) { debug( `path = ${path} ; value = ${JSON.stringify( value - )} (${typeof value}) ; (subj type: ${ - subj.length ? 'list' : 'hash' + )} (${typeof value}) ; (subj type: ${subj.length ? 'list' : 'hash' }) ; newValue = ${JSON.stringify(newValue)} ; newPath = ${newPath}` ); @@ -572,7 +571,7 @@ function dummyParser(body, callback) { } // doc is a JSON object -function extractJSONPath(doc, expr) { +function extractJSONPath(doc, expr, opts) { // typeof null is 'object' hence the explicit check here if (typeof doc !== 'object' || doc === null) { return ''; @@ -581,7 +580,7 @@ function extractJSONPath(doc, expr) { let results; try { - results = jsonpath(expr, doc); + results = jsonpath({ path: expr, json: doc, wrap: opts.multiple ?? true }); } catch (queryErr) { debug(queryErr); } @@ -590,6 +589,10 @@ function extractJSONPath(doc, expr) { return ''; } + if (opts.multiple === false) { + return results; + } + if (results.length > 1) { return results[randomInt(0, results.length - 1)]; } else {