Reputation: 83
I am working on a node project that needs a list of JSON objects. At first, I copy and pasted the entire list of JSON into the main app.js file but clearly that isn't conventional. I made a new json file with all the objects I need in my models folder and am accessing it like this
var subjects = require('./models/courses.json');
When I console.log this, it throws an error
Unexpected token , in JSON at position 52
I basically just want to be able to make subjects store an array of json objects.
Here is my json file
{"id" : "AAFS", "department" : "Africana Studies"},
{"id" : "AANT", "department" : "Anthropology"},
{"id" : "AARA", "department" : "Arabic"},
{"id" : "AARH", "department" : "Art History"},
{"id" : "AART", "department" : "Art"}
Upvotes: 1
Views: 15828
Reputation: 79
If you want to create a JSON with a set of objects, you need to create an array to wrap the objects, so I suppose this should solve your problem:
[
{"id" : "AAFS", "department" : "Africana Studies"},
{"id" : "AANT", "department" : "Anthropology"},
{"id" : "AARA", "department" : "Arabic"},
{"id" : "AARH", "department" : "Art History"},
{"id" : "AART", "department" : "Art"}
]
Upvotes: 2
Reputation: 3478
Looks like you have poorly formed JSON.
Paste it here to see if it validates: https://jsonformatter.curiousconcept.com/
Chances are that it is whatever comes before that comma.
Upvotes: 2
Reputation: 55
The error is pointing you to the issue. There is an error in your JSON syntax. The way you are importing the file is fine.
Look back over your file. Or if it helps use a test JSON file where you know the formatting is correct and the error should disappear.
Upvotes: 1