2

I'm getting a it confused here. In a VueJS project I'm using a dummy json data in the following way :

import data from './assets/data.json'

which i can than print to the screen, for example : {{Object.keys(data)}}

In this question i find different answers, some claim you cant import json files, some claim you can . common sense implies that i can import them since it is working in my project, now the question is: is this some Webpack magic helping me to achieve this import or any other behavior taking place under the hood? or is ES6 actually allows me to import json as presented above?

Community
  • 1
  • 1
yariv bar
  • 936
  • 3
  • 20
  • 39
  • ES6 only specifies importing ES6 modules (scripts with `export` declarations and stuff), so yes this a feature of your module loader. – Bergi Jan 09 '19 at 08:14
  • typically you would use the fetch api. here is an example with vue and axios: https://vuejs.org/v2/cookbook/using-axios-to-consume-apis.html – smoore4 Jan 09 '19 at 08:46
  • You can but you should not as it may be a problem when you are reading and also writing in JSON file in run time. if you will read JSON using import then updated data will not reflect in your object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import – AZ_ Jan 09 '19 at 09:34

1 Answers1

0

Please go through, How to import a json file in ecmascript 6?

Says,

ES6/ES2015 doesn't support loading JSON via the module import syntax

  • 2
    i actually intended to link to this question but mistakenly linked to another one. as you can see there, while the second answer by Simone claims "Unfortunately ES6/ES2015 doesn't support loading JSON via the module import syntax" , the 5th answer by @Wiliamli claims "In ES6/ES2015, you can import json file in your code." – yariv bar Jan 09 '19 at 08:43