Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Can't read my JSON file using require method in JS

I'm trying to loop through JSON data that I'm pulling from a file. I'm using the require function to get the JSON file but it keeps telling me there is an unexpected '{' token? positions on IDE and console are a little off but I'm pretty sure it's the second objects opening { -position 44-

Any suggestions?

myData.json

{
   "colour" : "blue",
   "cm"     : 2
}

{
   "colour" : "red",
   "cm"     : 5
}

index.js

const fileContents = require("./myData.json");
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because the JSON is invalid. Your JSON file needs to define one entity, where currently you're defining two. Even if the code could read the data, there's no logical way to indicate which of those two objects you want to refer to.

It looks like you meant to put them in an array:

[
  {
    "colour" : "blue",
    "cm"     : 2
  },
  {
    "colour" : "red",
    "cm"     : 5
  }
]

Then you can import the whole array, and refer to elements of the array for each object.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!