question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON.parse() throws errors when response contains an array

See original GitHub issue

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.

https://github.com/cnguy/kayn/blob/c7fb8edb2f41cb3709fba7baa7f9d74e3455bc43/lib/RequestClient/Request.js#L101

let res = await this.limiter.executing({
...
try { 
  if (Array.isArray(res)) {
    res = JSON.stringify(res)
   }
   const blob = JSON.parse(res)
...
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
srcpackcommented, Jan 26, 2018

It works. I’ll leave the issue open and leave it you to close when you’re ready. Thanks.

0reactions
cnguycommented, Jan 26, 2018

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).

 const provider = await kayn.TournamentStub.registerProviderData(
    REGIONS.NORTH_AMERICA,
    'https://localhost/cb',
)
const tournament = await kayn.TournamentStub.register(provider, 'kappa')
const codes = await kayn.TournamentStub.create(tournament, {
    mapType: 'SUMMONERS_RIFT',
    metadata: 'Kappa',
    pickType: 'TOURNAMENT_DRAFT',
    spectatorType: 'NONE',
    teamSize: 5,
}).query({ count: 5 })
``
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found