Cannot pass JSON data to JSON.parse() in TypeScript
TypeScript throws an error when using JSON.parse(). When I pass it this json data:
{
"AXzgeQ7vXOjWnFrgjHiM": {
"0-17-13955610754117-8-2021": {
"channelId": "17",
"end": "1642091400000",
"id": "0-17-13955610754117-8-2021",
"start": "1642090860000",
"title": "Yle Uutiset Alueeltasi",
"type": "broadcast"
}
}
}
Which is imported like: import stubReminders from './stub_data.json';
It throws the error:
Argument of type '{ AXzgeQ7vXOjWnFrgjHiM: { "0-17-13955610754117-8-2021": { channelId: string; end: string; id: string; start: string; title: string; type: string; }; }; }' is not assignable to parameter of type 'string'.ts(2345)
What should I change to successfully use JSON.parse()
?
Try accessing your imported stubReminders
as is without trying to parse it. I have tested out an example, and the import parses the JSON automatically.
I hope this works.
Also checkout this codesandbox link.