S
S
sunny-plum2020-07-01 05:30:41
JavaScript
sunny-plum, 2020-07-01 05:30:41

How to correctly read the JSON file so that it is possible to output country and id?


JSON file

[
    {
      "id": 833,
      "name": "Ḩeşār-e Sefīd",
      "state": "",
      "country": "IR",
      "coord": {
        "lon": 47.159401,
        "lat": 34.330502
      }
    },
    {
      "id": 2960,
      "name": "‘Ayn Ḩalāqīm",
      "state": "",
      "country": "SY",
      "coord": {
        "lon": 36.321911,
        "lat": 34.940079
      }
    },
    {
      "id": 3245,
      "name": "Taglag",
      "state": "",
      "country": "IR",
      "coord": {
        "lon": 44.98333,
        "lat": 38.450001
      }
    }
]


.js file

let test = JSON.parse('js/test.json');
console.log(test);


I get this error:

VM119:1 Uncaught SyntaxError: Unexpected token j in JSON at position 0
at JSON.parse ()
at script.js:1
(anonymous) @ script.js:1


I saw the answer that the file is already parsed, if such error, but then I still don’t understand how to connect it and pass it to a variable. Tell me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question