1

I'm currently working on a web scraper and I'm getting the JSON from <script type="application/ld+json> on a particular web page. I get it as a string using Cheerio and pass it on a JSON parser (npm package). But I keep on getting a syntax error and this happens if the value has a trailing white space.

I tried the reviver by trimming each value but it still doesn't work.

This is a snippet of my JSON string where I get a Syntax Error:

{"...821", "description":"                                                  \r\n
                                ","@type":"Organization",...}

This is the error that I'm getting:

ErrorEXError [JSONError]: Unexpected token       in JSON at position 1432 while parsing near '...821","description":"                                                \r\n                                             ","...'

How can I trim the description value without string manipulation?

1
  • without string manipulation? Huh? You'll have to manipulate the string somehow in order to change it to make it parseable, what do you mean? Commented Sep 25, 2019 at 1:57

1 Answer 1

2

A properly formatted JSON string must not contain any literal newline characters - it can only contain representations of newline characters, such as with \r\n. Replace all literal newlines with \n, and you should be able to parse it properly:

const jsonStr = `{"description":"                                                  \r\n
                                ","@type":"Organization"}`;
const jsonStrWithoutNewlines = jsonStr.replace(/[\n\r]+/g, '\\n');
const obj = JSON.parse(jsonStrWithoutNewlines);
console.log(obj);

Literal tab characters aren't permitted either - if that's an issue, replace them with \t:

const jsonStr = `{"description":"                                                  \r\n
                                ","@type":"Organization			"}`;
const jsonStrWithoutNewlines = jsonStr
  .replace(/[\n\r]+/g, '\\n')
  .replace(/\t/g, '\\t');
const obj = JSON.parse(jsonStrWithoutNewlines);
console.log(obj);

Sign up to request clarification or add additional context in comments.

1 Comment

Yet another example how JSON is overused for “human editable” configuration files..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.