3

In general as noted here it's pretty straight forward:

import data from './data.json';

However this does not work in Stackblitz Angular projects.

The linked project logs the following:

preview-3a0c9433aa42f56dbd90b.js:1 SyntaxError: Unexpected token o in JSON at position 1
3
  • 1
    why not put that json in a constant in a ts file? Commented Dec 26, 2018 at 18:39
  • I think that's a good idea ... Just curious whether there is a simple way to also import it in general? Commented Dec 26, 2018 at 18:40
  • 1
    I would suggest getting on their discord, they are pretty active there. Commented Dec 26, 2018 at 18:42

1 Answer 1

5

When importing, you should not use JSON.parse(). It's already parsed by cli/webpack

import data from './data.json';

test: any = data;

stack

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

2 Comments

@pierreDuc doesn't that SB use a .ts file with export const data = {... ?
@BenRacicot you are absolutely right, I guess I saved the stack at a wrong point when testing. I've updated it now

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.