最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to import JSON file to javascript array - Stack Overflow

programmeradmin1浏览0评论

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"}

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"}
Share Improve this question edited Nov 20, 2017 at 22:56 John A asked Nov 20, 2017 at 22:26 John AJohn A 831 gold badge3 silver badges13 bronze badges 3
  • 1 Just read it using fs functions. – zerkms Commented Nov 20, 2017 at 22:28
  • Would you mind posting the JSON file? – Gerrit Luimstra Commented Nov 20, 2017 at 22:32
  • That error indicates your file isn't proper JSON. Note JSON is stricter than JavaScript object literals. You can't have trailing mas, keys must be enclosed in strings, and a whole host of stricter rules. To convert an object to JSON, use JSON.stringify. – vox Commented Nov 20, 2017 at 22:36
Add a ment  | 

3 Answers 3

Reset to default 2

Looks like you have poorly formed JSON.

Paste it here to see if it validates: https://jsonformatter.curiousconcept./

Chances are that it is whatever es before that ma.

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"}
]

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.

发布评论

评论列表(0)

  1. 暂无评论