-
Notifications
You must be signed in to change notification settings - Fork 108
Closed
Description
Describe the bug
On invalid JSON strings an error is thrown.
To Reproduce
Steps to reproduce the behavior:
- Retrieve https://aersf.com/products/pro-kit
Expected behavior
Invalid JSON-LD strings are ignored.
Actual behavior
Error parsing JSON-LD SyntaxError: Expected ',' or ']' after array element in JSON at position 1962
at JSON.parse (<anonymous>)
at Element.<anonymous> (/Users/nick/Development/price-tracker/node_modules/open-graph-scraper/dist/esm/lib/extract.js:106:51)Screenshots
N/A
Additional context
Add any other context about the problem here.
- OS: macOS
- Node Version: 20
- openGraphScraper Version: 6.8.3
- tsconfig.json: N/A
Potential solution
Catch the error to allow other valid JSON-LD and Open Graph data on the page to be parsed.
diff --git a/lib/extract.ts b/lib/extract.ts
--- a/lib/extract.ts (revision 686fcc5d67a5cb2999e24b05525032a183d38ecc)
+++ b/lib/extract.ts (date 1737082038030)
@@ -99,7 +99,11 @@
if (scriptText) {
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
scriptText = unescapeScriptText(scriptText);
- ogObject.jsonLD.push(JSON.parse(scriptText));
+ try {
+ ogObject.jsonLD.push(JSON.parse(scriptText));
+ } catch (e) {
+ console.error('Error parsing JSON-LD', e);
+ }
}
}
});Metadata
Metadata
Assignees
Labels
No labels