Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda