Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to export this JSON data for importing in the project however it shows unexpected syntax error

When it was in the same file as component worked perfectly. Now I am trying to exclude it from component to make project cleaner. However it shows unexpected syntax errors. File format is JSON. How can this code be fixed and work as it needs to be?

const postsData = [

      {
        id: 1,
        title: "How to start a business with 100$",
        published: "14h ago",
        image: require("../img/img1.jpg"),
      },
      {
        id: 2,
        title: "Get funding for your startup",
        published: "19h ago",
        image: require("../img/img2.jpg"),
      },
      {
        id: 3,
        title: "Latest Fashion Trends for 2018",
        published: "14h ago",
        image: require("../img/img3.jpg"),
      },

    ]

    export {postsData};

1 Answers

Try this:

export default [
      {
        id: 1,
        title: "How to start a business with 100$",
        published: "14h ago",
        image: require("../img/img1.jpg"),
      },
      {
        id: 2,
        title: "Get funding for your startup",
        published: "19h ago",
        image: require("../img/img2.jpg"),
      },
      {
        id: 3,
        title: "Latest Fashion Trends for 2018",
        published: "14h ago",
        image: require("../img/img3.jpg"),
      }
    ]

and import it like

import postsData from 'filepath';

PS:

  • there is no naming convention for postsData you can name it anything.
  • your json file should have an extension .js for your case because you're not using JSON objects
like image 56
Ahsan Ali Avatar answered Dec 07 '25 15:12

Ahsan Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!