3

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};
Artur
  • 351
  • 5
  • 17

7 Answers7

2

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
Ahsan Ali
  • 4,223
  • 2
  • 14
  • 25
0

You need to add double "" mark like this below

{
"id": "1",
"title": "How to start a business with 100$",
"published": "14h ago",
}

please use json lint to validate json- json lint

please follow

[{
        "id": "1",
        "title": "How to start a business with 100$",
        "published": "14h ago",
        "images": [{
                    "bannerImg1": "./images/effy.jpg"
                },
                {
                    "id": "2",
                    "title": "Get funding for your startup",
                    "published": "19h ago",
                    "images": [{
                            "bannerImg1": "./images/effy.jpg"
                        },
                        {
                            "id": "3",
                            "title": "Latest Fashion Trends for 2018",
                            "published": "14h ago",
                            "images": [{
                                "bannerImg1": "./images/effy.jpg"
                            }]

                        }
                    ]
Ved Prakash
  • 1,390
  • 5
  • 22
  • 31
0

If it is a JSON file like MyFile.json, then you cant export any thing from JSON file with your current code as it contains keywords like export or const and you cant use JavaScript keywords in JSON file.

You can change the extension of the file to MyFile.js

OR

You can create a json file like MyFile.json and put only JSON code

{
"id": "1",
"title": "How to start a business with 100$",
"published": "14h ago",
}

and require it from JS file like : import data from './MyFile.json'

Ayush Nawani
  • 614
  • 5
  • 14
0

This is not a JSON, it's just a javascript file (according to syntax).
The last line constructs object with shorthand property and exports it in the form export { name1, name2, …, nameN }; where name1, name2, ..., nameN are named properties.
To import named property use following syntax:

import { export } from "module-name";

In your case it would be

import { postsData } from "<your file without js ext>"
Teivaz
  • 4,616
  • 4
  • 25
  • 63
0

As said above,this is not a JSON but just a Object.

First,New a js file,you can name this file whatever you want.for example,data.js

Then write like this in data.js:

export defult{
  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"),
      },
   ]
}

Finally,in your component file:

import {xxxx} from './data';

in this way,you can get your data,xxxx.postsData.

木大白易
  • 424
  • 4
  • 13
0
with commonjs module
const express = require("express");
const config = require("./config.json");

typescript

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true
  }
}

import law from './keywords/law.json'
import special from './keywords/special.json'
David Buck
  • 3,439
  • 29
  • 24
  • 31
-1

If you have .JSON file and everything inside it is json then you need not export it like normal module. You simply import it in other module and use it like normal object. Thats it !

pantech
  • 69
  • 2
  • 6