Skip to content Skip to sidebar Skip to footer

How To Import A File With Json Object To Another Js File?

I have a js file with values. Usually, i export it from that data js file to another file. But currently, i am getting error as i am trying to export file which has json object. Us

Solution 1:

enter image description here

Inside js object keyvalue pairs must have : not =

index.js

exports.data = {
price: [
    {
      "film":
      {
        F: [`a`, `b`, `c`],
        I: [`d`, `e`, `f`],
        L: [`g`, `h`, `i`],
      }
    }
  ]
}

read.js

// const data = require('./index').data;

// or

const {data} = require('./index');

console.log(data.price)

Solution 2:

You're mistake is how you're assigning the price variable in filmdata.js. Since the variable is in an object, you must use a colon to assign it a value: price: <value>.

So filmdata.js would be something like:

exports.Category={
  price: [{
    "film": {
       F: [1, 2, 3],
       I: [4, 5, 6],
       L: [9, 8, 7],
      }
    }
  ]
}

And whatever other data comes after it.


Post a Comment for "How To Import A File With Json Object To Another Js File?"