JSON.parse() throws errors when response contains an array
See original GitHub issueIssue Description
I haven’t done much testing but some endpoints seem to respond with arrays which cause JSON.parse() to fail. For example: ChampionMastery.list returns a stringified array that parses with no issue. TournamentStub.create returns a normal array that throws errors in JSON.parse().
I’m not sure if it’s Riots fault or something in the lib causing it but I’m using this workaround for now. I haven’t tested it with every endpoint but checking for array and calling stringify on it seems to be a working bandaid until a permanent solution is found.
let res = await this.limiter.executing({
...
try {
if (Array.isArray(res)) {
res = JSON.stringify(res)
}
const blob = JSON.parse(res)
...
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >parsing JSON array gives error - Stack Overflow
Normal JavaScript arrays are designed to hold data with numeric indexes. Try using Object instead of an array. Try using the below code...
Read more >SyntaxError: JSON.parse: bad parsing Breaking Your Code ...
The JSON Parse error, as the name implies, surfaces when using the JSON.parse() method that fails to pass valid JSON as an argument....
Read more >How to parse a JSON Array in JavaScript | bobbyhadz
Use the `JSON.parse()` method to pase a JSON array, e.g. `JSON.parse(arr)`. The method parses a JSON string and returns its JavaScript value or...
Read more >Parsing JSON that either includes an array or an object
Solved: I need to parse a JSON message from an external system. The data contains an activity and 1..n activity SLA records.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It works. I’ll leave the issue open and leave it you to close when you’re ready. Thanks.
It should work now. 😃 https://github.com/cnguy/kayn/commit/380f5db67dca8ff93335c8b2aa3bf2483f21aa2e
The fork dependency from the git master branch URL has been moved to the npm registry, so I won’t be accidentally breaking any projects now haha.
This fix is in kayn@v0.8.4 so make sure to upgrade! Let me know if it works, and if it does,
feel free to close the issue.edit: Actually, it might be better to leave this issue up for a while just in case anyone else stumbles upon problems with the old versions or problems with .create like I did before you told me about it.
The code below should work cleanly (and the ChampionList example above should work with the right API key too).