-1

This is my Json file data.json i want this data to be fetched by either fetch or axios and return the data to variable

{"list":[
  {
    "rank":"1",
    "points":"1025",
    "name":"John Doe",
    "age":"27"
 },
 {
   "rank":"3",
   "points":"245",
   "name":"Elizabeth",
   "age":"17"
 },
 {
    "rank":"2",
     "points":"566",
     "name":"Samantha",
     "age":"22"
 }]}

I want this Whole data assigned to a variable the Output should be

const data=   {"list":[
        {
         "rank":"1",
         "points":"1025",
         "name":"John Doe",
         "age":"27"
       },
       {
        "rank":"3",
        "points":"245",
        "name":"Elizabeth",
        "age":"17"
       },
       {
        "rank":"2",
        "points":"566",
        "name":"Samantha",
       "age":"22"
        }]}
phuzi
  • 12,078
  • 3
  • 26
  • 50
Selva
  • 9
  • 1
  • 1
    This has already been answered here: https://stackoverflow.com/questions/51859358/how-to-read-json-file-with-fetch-in-javascript – eselskas May 23 '22 at 11:24
  • Welcome to Stack Overflow! You are encouraged to make an attempt to write your code. If you encounter a specific technical problem during that attempt, such as an error or unexpected result, we can help with that. Please provide specific information about that attempt and what didn't work as expected. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David May 23 '22 at 11:28
  • Does this answer your question? [How to read JSON file with fetch() in javascript?](https://stackoverflow.com/questions/51859358/how-to-read-json-file-with-fetch-in-javascript) – Peter Seliger May 23 '22 at 11:38

1 Answers1

0
await fetch("/path/to/data.json")
.then(res => res.json())
.then((json) => {
        console.log(json)

});
Chad
  • 376
  • 2
  • 14
  • if i do this the promise rejects and get an error like this " VM157:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 " – Selva May 23 '22 at 11:38
  • @Selva: That happens when you're trying to parse HTML data as JSON. You can use your browser's debugging tools to see what data is actually being returned from the server for the AJAX operation. – David May 23 '22 at 11:49
  • @David: I got you it returns my index.html I guess my path to json file is wrong. – Selva May 23 '22 at 12:11