re: How can I read a local JSON file in HTML/JavaScript? VIEW POST


The straightforward answer to your question is that you can't (yet). You a few options for storing and loading data from the browser:

  • Server requests -- AJAX requests with fetch or websocket connections for smaller messages and real-time communication
  • Local data storage within the browser -- as of yet there are no standard APIs for local filesystem access (though it's in the works); the two main options are:
    • localStorage -- simple key-value storage that can be used with JSON.stringify() to store objects; can be a little dangerous if you store a ton of data there because it's synchronous and can lock up the main thread, but for smaller objects it's just fine
    • indexedDB -- more like a database, asynchronous so it doesn't lock up the browser, but has a famously clunky API that takes some getting used to; there are Promise-based wrapper libraries that make it much easier though
code of conduct - report abuse