0

I am reading in a JSON file and attempting to convert them to an array of objects. I get a syntax error on the component file.

This is the component code

import * as blogs from '../story/stories.json';
import { IStory } from '../models/story';

@Component({
  selector: 'app-story',
  templateUrl: './story.component.html',
  styleUrls: ['./story.component.scss']
})
export class StoryComponent implements OnInit {
  stories: IStory[] ;
  constructor() { }

  ngOnInit() {
    this.stories = blogs; <-- syntax error on this line
      
  }

this is the story.ts file:

export interface IStory {
    id: number;
    author: string;
    title: string;
    pictureUrl: string;
    body: string;
  }

This is the JSON file, stories.json:

[
{"date":3,"author":"AVC", "pictureUrl":"Scorching", "title":"Scorching", "body":"Scorching"},
{"date":4,"author":"ADF", "pictureUrl":"Scorching", "title":"Scorching", "body":"Scorching"}
]
3
  • Waht is error ? Where is blogs coming? Commented Jun 26, 2020 at 21:36
  • @pc_coder this is the line: import * as blogs from '../story/stories.json'; Commented Jun 26, 2020 at 21:43
  • I did console.log(blogs) and it is printed out as the json file Commented Jun 26, 2020 at 21:44

1 Answer 1

1

Demo try to map to interface

this.stories = this.blogs.map(x=> {return {author:x.author,title:x.title,pictureUrl:x.pictureUrl,body:x.body,id:null}});
Sign up to request clarification or add additional context in comments.

1 Comment

I changed the this.blogs to blogs . it works. Thanks

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.