4

I reused some code that referenced an object in a .json file. The .json file was as follows:

{
  "a": {
    "b": "Hello world"
  }
}

The original code referenced it as follows:

import * as newObj from "./test.json"

This didn't create the object correctly (although, it strangely ran perfectly in the original application". Changing it to the following worked:

import newObj from "./test.json"

Could anyone shed some light on how these two imports would differ, and possibly why it worked in the original form in one app, but needed to be changed in the second?

I have seen some answers around "import x vs import * as x" - but this question pertains specifically to how a json object is structured when imported in these 2 different ways.

BruceM
  • 1,549
  • 1
  • 18
  • 41
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import – ellipsis Jan 15 '19 at 05:11
  • 1
    Possible duplicate of [Difference between import X and import \* as X in node.js (ES6 / Babel)?](https://stackoverflow.com/questions/31386631/difference-between-import-x-and-import-as-x-in-node-js-es6-babel) – ellipsis Jan 15 '19 at 05:13
  • Not a duplicate, specifically want to know about how import handles json objects – BruceM Jan 15 '19 at 06:42
  • Importing .json is not a part of the JS language, but a convenient feature provided by your module loader (i.e webpack, parcel, etc. ). Therefore the reasons why one syntax works in one project, but doesn't in another, depends entirely on the toolings. – Derek Nguyen Jan 15 '19 at 14:44
  • Thanks, that's helpful - i am guessing that a webpack difference is then the issue at hand – BruceM Jan 15 '19 at 15:21
  • Yeah, I bet we can pin point down the cause if you share the webpack version & configs of both projects! – Derek Nguyen Jan 16 '19 at 01:09

0 Answers0