Skip to content

JSON parsing error thrown on invalid JSON-LD #257

@nickmorri

Description

@nickmorri

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);
+          }
         }
       }
     });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions