From b3399927e1c5da61642838cf6c597eca44b8f6b4 Mon Sep 17 00:00:00 2001 From: Adler Faulkner Date: Wed, 6 Jul 2022 17:13:04 -0700 Subject: [PATCH] only loop over array if not nested --- src/input-parser/JSONParser.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/input-parser/JSONParser.js b/src/input-parser/JSONParser.js index 5e8943b..d8f2cbe 100644 --- a/src/input-parser/JSONParser.js +++ b/src/input-parser/JSONParser.js @@ -15,18 +15,17 @@ class JsonParser { getData(index, selector) { const sel = selector.replace(/^PATH~/, ''); const splitter = sel.startsWith('[') ? '' : '.'; - return JSONPath({ + const arr = JSONPath({ path: `${this.paths[index]}${splitter}${sel}`, json: this.json, resultType: selector.startsWith('PATH~') ? 'pointer' : 'value', }) - .filter((e) => e !== null && e !== undefined) // null values are ignored (undefined shouldn't happens since input is json) - .map((e) => { - if (Array.isArray(e)) { - return e.map((eItem) => eItem.toString()); - } - return e.toString(); - }); // return only strings + .filter((e) => e !== null && e !== undefined); // null values are ignored (undefined shouldn't happens since input is json) + + if (arr.length === 1 && Array.isArray(arr[0])) { + return arr[0].map((e) => e.toString()); + } + return arr.map((e) => e.toString()); } }