D
D
DevilPrado2021-05-11 16:23:23
JavaScript
DevilPrado, 2021-05-11 16:23:23

How to save json to a variable?

There is a file located in the root of the site with the json extension.
It is necessary to shove it into a variable and parse it. I do not quite understand the process of obtaining this file in js.
I know how to parse, but I don’t understand how to specify the path to the file to be read?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arseny, 2021-05-11
@DevilPrado

If we are talking about building a project or working in a nodejs environment, then a simple require with a link and assigning it to a variable is enough. If the project is implemented in the format of a “regular site”, then fetch will help, which should request a file using a “server” link. Let's say the file is at the root of the project in the assets/json directory, then the link will look something like https://domain.name/assets/json/file.json . Provided, of course, that the server will give the file and the entry point to the domain matches the project root, as I gave in the example.

R
rPman, 2021-05-11
@rPman

JSON.parse (string with json)
load the string using ajax (by the way, there is an option to parse automatically)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question