Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parsing error thrown on invalid JSON-LD #257

Open
nickmorri opened this issue Jan 17, 2025 · 3 comments · May be fixed by #259
Open

JSON parsing error thrown on invalid JSON-LD #257

nickmorri opened this issue Jan 17, 2025 · 3 comments · May be fixed by #259

Comments

@nickmorri
Copy link

Describe the bug
On invalid JSON strings an error is thrown.

To Reproduce
Steps to reproduce the behavior:

  1. 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);
+          }
         }
       }
     });
@nickmorri
Copy link
Author

Let me know if the proposed change looks OK and I can open a PR.

@jshemas
Copy link
Owner

jshemas commented Jan 17, 2025

That looks good to me. Please add a unit test for this use case when you open the PR.

@nickmorri
Copy link
Author

Great, just sent #259 for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants