Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading