B
B
broke2021-09-12 17:48:58
JavaScript
broke, 2021-09-12 17:48:58

How to get data from JSON file?

I have a JSON file with data in my project folder. Based on this data, I need to build a table. I basically thought over the construction logic, but I don’t understand how to get data from a JSON file. Example:
JSON:

[
  {
    "id": "5c2286fb23e87be312d55d9a",
    "name": {
      "firstName": "Brooks",
      "lastName": "Stone"
    },
    "phone": "+7 (843) 431-2190"
]

I want to assign this data array to a variable in my script. How to properly parse a local JSON file in my script.js? Do I need to use a local server for this? Can this be done via fetch(), without ls?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
low molecular macro, 2021-09-12
@molekulyarniy

Local file? Install Node!
UPD

  1. run your script.js in Node environment
  2. in the script itself, read the local db.json file
  3. turn json from file into array withconst array = JSON.parse(ваша_json_строка)
  4. create a dynamic table based on the resulting array!

UPD2
Perhaps I hurried with the answer. Try this option . But you have to solve problems with CORS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question