Skip to content

Commit 9344106

Browse files
committed
feat: if value-type object and parse fails snitizze to remove any string outside of object and attempt parse again
1 parent f625b4f commit 9344106

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/getValue.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,24 @@ const getValue = (element) => {
309309
value = [value];
310310
}
311311

312-
if (value && (valueType == 'object' || valueType == 'json')) {
312+
if (value && (valueType === 'object' || valueType === 'json')) {
313313
try {
314-
value = JSON.parse(value)
314+
value = JSON.parse(value);
315315
} catch (error) {
316-
value = value
316+
// Fallback to regex extraction if JSON parsing fails
317+
const jsonRegex = /(\{[\s\S]*\}|\[[\s\S]*\])/;
318+
const match = value.match(jsonRegex);
319+
320+
if (match) {
321+
try {
322+
value = JSON.parse(match[0]);
323+
} catch (e) {
324+
// If parsing still fails, keep the original value or handle the error
325+
console.error('Failed to parse JSON after regex extraction:', e);
326+
}
327+
} else {
328+
console.error('No valid JSON structure found in the string.');
329+
}
317330
}
318331
}
319332

0 commit comments

Comments
 (0)