0

Error I am getting:

SyntaxError: unexpected token: ':'

This is what is in the JSON file:

{ "Name":"John", "Age":30 }

This is what is in the JavaScript file reading the data:

var jsonFile = JSON.parse(test);
var Tag = document.getElementById("test");

Tag.innerHTML = jsonFile.Name;

This is the HTML tags I have added in the HTML file:

<script type="text/javascript" src="../static/test.json" async></script>
<script type="text/javascript" src="../static/java.js" async></script>

Why am I getting this error?

Callum S
  • 213
  • 2
  • 10

2 Answers2

1

JSON.parse is expecting a string such as

'{ "Name":"John", "Age":30 }'

Try the following for reading a local json file

haidousm
  • 556
  • 6
  • 21
0

You can use .json() instead of parse().

test.json()
Minal Shah
  • 1,402
  • 1
  • 5
  • 13
  • Still gives the the same error, sorry, it seems to be a error with the json file not the javascript – Callum S Aug 04 '20 at 15:07