Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

Try:

const parsedData = JSON.parse(data)
const address = parsedData.address
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading