1
const response = await fetch('https://api.codetabs.com/v1/proxy/?quest=http://hamsterland.herokuapp.com/api/users?id=330746772378877954');
const json = await response.json(); 
const user: User = JSON.parse(json);
interface User {
    id: string;
    username: string;
    avatar: string;
}

On the last line, JSON.parse(json), I receive an error that states:

Unhandled Rejection (SyntaxError): Unexpected token o in JSON at position 1

I am not sure what is causing this. The interface model matches the JSON structure.

3
  • 4
    It's already parsed. That's what .json() does. Commented Dec 3, 2020 at 23:28
  • paste.mod.gg/akepubiwix.json @Nonik Commented Dec 3, 2020 at 23:29
  • 2
    The error you are seeing is because you are trying to parse an already parsed json string. So when you run JSON.parse(object) it has to try to figure out what to do, so it to strings the object which turns into JSON.parse('[object Object]') Commented Dec 3, 2020 at 23:30

1 Answer 1

4

Looks like the JSON.parse(json) was unnecessary. All I had to do was

const response = await fetch('https://api.codetabs.com/v1/proxy/?quest=http://hamsterland.herokuapp.com/api/users?id=330746772378877954');
const user: User = await response.json(); 

Thank you @Taplar for the solution.

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

1 Comment

this is not deserialization -1

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.