A
A
akopartem2021-01-09 19:49:17
JavaScript
akopartem, 2021-01-09 19:49:17

How to use file in JSON.parse?

I have a JSON file that changes periodically. How can it be used in JSON.parse without copying the contents of the file every change?

const shop = JSON.parse('/shop.json')

document.write(JSON.stringify(shop.data.featured.name))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-01-09
@sergiks

Don't use document.write()it - it's evil.
In the markup, insert an empty div where the content will be inserted: Loading is not instant, asynchronous:<div id="shopdata"></div>

fetch('/shop.json')
  .then(response => response.json())
  .then(data => document.getElementById("shopdata")
    .innerHTML = JSON.stringify(data.data.featured.name))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question