get data from json file in javascript

To get data from a JSON file in Javascript, you can use the fetch() function to asynchronously read the file and retrieve its contents.

index.tsx
fetch('data.json')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));
132 chars
5 lines

This code retrieves the contents of data.json, parses it as JSON, and logs the resulting data to the console. If an error occurs during this process, it is caught and logged to the console as well.

Note that this code uses promises to handle the asynchronous nature of file reading. The fetch() function returns a promise that resolves to a Response object, which can then be converted to JSON using the .json() method. The resulting promise resolves to the parsed JSON data.

gistlibby LogSnag