Skip to content
Advertisement

Parsing error message using substr and indexOf not working

I’m trying to extract a substring from an error message I receive and parse it to JSON. However it seems something with the indexOf or the substring method is not working as expected.

Here’s the full error message I’m trying to parse:

JavaScript

Basically I’m trying to extract this JSON string programatically:

JavaScript

To do this I wanted to use a combination of indexOf and the substring method:

JavaScript

Somehow it seems the find the correct start index but it misses the correct end and I have no idea why. Here’s the result:

JavaScript

If anyone has an idea what I’m doing wrong here and how to fix it, please let me know!

Advertisement

Answer

The javascript substr function takes 2 params.

First being the start index and second being the length of the string from the start index that you want to take.

Check the snippet.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement