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()?
The JSON.parse function takes a string as parameter. You are trying to pass an object to it. So you have to convert it to a string before, if you want to use JSON.parse.
But keep in mind, that the imported object already is an object, so I think there is no need to parse it.
const obj = {
"AXzgeQ7vXOjWnFrgjHiM": {
"0-17-13955610754117-8-2021": {
"channelId": "17",
"end": "1642091400000",
"id": "0-17-13955610754117-8-2021",
"start": "1642090860000",
"title": "Yle Uutiset Alueeltasi",
"type": "broadcast"
}
}
}
console.log(JSON.parse(JSON.stringify(obj)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With