Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
How to read specific object from json file in JavaScript?

I'm trying to work with a json file but I can't figure out how to read just a specific object from a json file.

My current code looks like this:

try {
    const data = fs.readFileSync("addresses.json", "utf8");
    console.log(data);
} catch (err) {
    console.error(err);
}

This works fine. However when I try to get some object from the file like this:

console.log(data.address)

It doesn't work because it is a string.

So my question is how can I read just a single object from the json file.

Thanks a lot!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try:

const parsedData = JSON.parse(data)
const address = parsedData.address
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use JSON.parse(), as right now, you are trying to access a property off of a string. However, the method will change it to an object.

// Dummy JSON data
const data = `{ "address": "Stack Overflow" }`;

const json = JSON.parse(data);
console.log(json.address);

This will make it possible to access the properties off of the object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

you must convert the data into json like so:

    JSON.parse(data)

In your example :

    try {
        const data = fs.readFileSync("addresses.json", "utf8");
        console.log(JSON.parse(data).address);
    } catch (err) {
        console.error(err);
    }

I hope I answered your question. Feel free to comment if I misunderstood you or you have some questions ;)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda