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

144
Visualizações
how to read data from a json file in javascript

I have a json file with this object:

{
    "fontFamily": "Roboto",
    "color": "red",
    "backgroundColor": "green",
    "textForExample": "Hello world i'm a text that's used to display an exemple for my creator."
}

and when i'm parsing it I have an error in my console that says "infosFile.json:2 Uncaught SyntaxError: Unexpected token ':'" and then when I'm trying to use it in my Javascript, I got this message in console: "infosFile is not defined", I don't understand where is the problem

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The json is valid. You can check if you json file is valid at the following link: https://jsonformatter.curiousconcept.com/

The problem could be that in order to read json from your computer with JavaScript you need NodeJS installed. You can download NodeJS from here: https://nodejs.org/en/

You can read json with NodeJS like this.

const fs = require('fs');

let rawdata = fs.readFileSync('<path/yourFileName>.json');
let data = JSON.parse(rawdata);
console.log(data);
about 4 years ago · Juan Pablo Isaza Relatório

0

Make sure that you have the right path to access your JSON file

use require("") to access your json file and put your inside the double quotes

use JSON.parse() to read and store it inside a variable

const json_file = require("./json/test.json");
const data = JSON.parse(json_file);

You could also stringify it by using

const data_stringified = JSON.stringify(data);
about 4 years ago · Juan Pablo Isaza Relatório

0

Try

const info = require('infosFile.json');

console.log(JSON.parse(info)) // should output valid data
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