A
A
Alexander2020-05-28 20:30:52
JavaScript
Alexander, 2020-05-28 20:30:52

How to call value from JSON file?

I created a json file, put the values ​​I needed in it in this way:

{ "regions" : [
      "hongkong",
      "us-central", 
      "amsterdam", 
      "us-south", 
      "russia", 
      "us-east", 
      "south-korea", 
      "london", 
      "southafrica", 
      "europe", 
      "japan", 
      "eu-west", 
      "singapore", 
      "dubai", 
      "brazil", 
      "india", 
      "sydney", 
     "frankfurt", 
      "us-west"
    ] }

I need to call for example the value "frankfurt", and display it via alert()
How to do it?
Thanks in advance to everyone for the replies.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stepan Krapivin, 2020-05-28
@Alexandre888

const config = require("./config.json");

alert(config['regions'][17])

Y
Yura Komarov, 2020-05-28
@Yurajun

const config = JSON.parse('ваш json');
const ind = config.regions.indexOf('frankfurt');
console.log(config.regions[ind]);

G
GF, 2020-05-28
@fomenkogregory

For this task, json is not needed, it is better to use a regular object. But if you really want to (why?), then
https://stackoverflow.com/questions/19706046/how-t...
https://stackoverflow.com/questions/34944099/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question