0

Im getting a Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

var mydata = JSON.parse("file.json");
console.log(myJSON)

Below is an example of the data in the JSON file.

[[1,1,0,1,1,0,0,0,1,1,1,1,1,1],
[1,0,0,1,1,0,0,0,1,1,1,1,1,0],
[1,0,0,1,1,0,1,1,1,1,1,1,1,1],
[1,0,0,1,1,0,0,0,1,1,1,1,1,0], .... etc]

How can I retrieve the data from this JSON file in JS?

Is it not possible to write a JSON like this?

When I open the file in the browser, it looks like a neat JSON array?

browser JSON file

1 Answer 1

1

JSON.parse expects an actual JSON string instead of the file name, e.g. JSON.parse('{ "a": "b" }'). If you want to do it inside Node.js, you can use fs module to read file and parse it. In browser, you need to use file input to read the file first.

Sign up to request clarification or add additional context in comments.

2 Comments

ah oke! When i was using: $.getJSON("file.json", function(json) { console.log(json); }); I was getting a Cross-Origin-aanvraag blocked, so I thought i could also do it like above
Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.