Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 141980

How to access a JSON static file imported with Webpack?

$
0
0

I have this JSON static file in my project structure:

someFile.json

The JSON is an stringified version of an array of objects: [{...},{...},etc]

I'm importing it with Webpack (v 4.41.2) as:

import '../someFile.json';<----- This is in my App.js file

But now I want to access it inside my App so I can parse it back into array and use it. How can I do it?

This doesn't seem to work:

import myJSONfrom '../someFile.json';
const parsedJson = JSON.parse(myJSON);

function App() {
  // USE parsedJSON
}

I get this error:

Uncaught SyntaxError: Unexpected token u in JSON at position 0

JSON File content:

[{"code":"STOCK","date":"2016-05-19T00:00:00.000Z","open":1240,"max":12430,"min":1240,"avg":12140,"close":12140,"numTrades":12,"amountTraded":120,"volume":121400,"quoteFactor":100},...OTHER OBJECTS....]

Note: This is a React app.


Viewing all articles
Browse latest Browse all 141980

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>