2

I'm having data as shown below

    {
  "name": "test",
  "records": [
    {
      "position": 1,
      "data": {
        "employees": {
          "teams": [],
          "users": []
        },
        "address": "ABC 123 Street"
      }
     },
     {
      "position": 2,
      "data": {
        "employees": {
          "teams": [],
          "users": []
        },
        "address": "DEF 456 Street"
      }
     }
  ]
}

Now I would like to get the address of all the records but when you see the output I'm getting it as [object]. So can anyone let me know how do I get the address ?

This is my code:

const fs= require('fs');
const { isObject } = require('util');

function jsonReader(filePath,cb){
    fs.readFile(filePath, 'utf-8', (error, fileData) =>{
    if(error){
        return cb && cb(error);
    }
     try {
        const mydata = JSON.parse(fileData);
        return cb && cb(null,mydata);
     } catch (error) {
        return cb && cb(error);
        
     }
    });
}

jsonReader('./data.json',(error,data)=>{
    if(error){
        console.log(error);
    }else{
        console.log(data);
    }
})

Output:

{
  name: 'test',
  records: [ { position: 1, data: [Object] }, { position: 2, data: [Object] } ]
}
Kitty
  • 300
  • 3
  • 12

1 Answers1

1

Is this what you are looking for?

data = JSON.parse(`{
  "name": "test",
  "records": [{
      "position": 1,
      "data": {
        "employees": {
          "teams": [],
          "users": []
        },
        "address": "ABC 123 Street"
      }
    },
    {
      "position": 2,
      "data": {
        "employees": {
          "teams": [],
          "users": []
        },
        "address": "DEF 456 Street"
      }
    }
  ]
}`);

addresses = data.records.map(record => record.data.address);
console.log(addresses)
Michael M.
  • 10,486
  • 9
  • 18
  • 34