Home > Mobile >  NodeJS could not read [Object] in JSON data
NodeJS could not read [Object] in JSON data

Time:11-24

I have data as a JSON format

{"mac":"f008d155b4fe","deviceClass":["iBeacon","eddystone"],"model":"iBeacon","lastSeen":"1637655820","bevent":{"event":"update"},"rssi":{"last":-84},"beacons":[{"ibeacon":{"uuid":"783a69707094d0863b4a9bf94e555241","major":816,"minor":6,"power":0},"eddystone":{"power":0}}],"sensors":{"temperatureC":18,"voltage":3.299999952316284},"stats":{"uptime":"710","adv_cnt":"24","frame_cnt":7}},
        {"mac":"a0e6f8515eff","deviceClass":["arubaTag"],"firmware":{"bankA":"1.2-15"},"assetId":"0000-0000-0000","lastSeen":"1637655821","bevent":{"event":"update"},"rssi":{"last":-69},"sensors":{"battery":83},"stats":{"uptime":"82350","frame_cnt":3},"vendorName":"Aruba"}

enter image description here

I can print data from the level 1 but I could not print data in another level from JSON I would like to print value from beacons: [ [Object] ], Please give me a hints thank you

here is my code

const data = [
        {"mac":"f008d155b4fe","deviceClass":["iBeacon","eddystone"],"model":"iBeacon","lastSeen":"1637655820","bevent":{"event":"update"},"rssi":{"last":-84},"beacons":[{"ibeacon":{"uuid":"783a69707094d0863b4a9bf94e555241","major":816,"minor":6,"power":0},"eddystone":{"power":0}}],"sensors":{"temperatureC":18,"voltage":3.299999952316284},"stats":{"uptime":"710","adv_cnt":"24","frame_cnt":7}},
        {"mac":"a0e6f8515eff","deviceClass":["arubaTag"],"firmware":{"bankA":"1.2-15"},"assetId":"0000-0000-0000","lastSeen":"1637655821","bevent":{"event":"update"},"rssi":{"last":-69},"sensors":{"battery":83},"stats":{"uptime":"82350","frame_cnt":3},"vendorName":"Aruba"}
  ];
  
  const arr1 = data.filter(d => d.mac == "f008d155b4fe");
  console.log('arr1', arr1);
  for (k in arr1.beacons) {
    console.log(k, ":", arr1.beacons[k]);
}

CodePudding user response:

This worked for me, I made it all an object and now you can access the beacons by doing newData.devices[0].beacons[0].(property)

const newData = {"devices": [{"mac":"f008d155b4fe","deviceClass":["iBeacon","eddystone"],"model":"iBeacon","lastSeen":"1637655820","bevent":{"event":"update"},"rssi":{"last":-84},"beacons":[{"ibeacon":{"uuid":"783a69707094d0863b4a9bf94e555241","major":816,"minor":6,"power":0},"eddystone":{"power":0}}],"sensors":{"temperatureC":18,"voltage":3.299999952316284},"stats":{"uptime":"710","adv_cnt":"24","frame_cnt":7}},         {"mac":"a0e6f8515eff","deviceClass":["arubaTag"],"firmware":{"bankA":"1.2-15"},"assetId":"0000-0000-0000","lastSeen":"1637655821","bevent":{"event":"update"},"rssi":{"last":-69},"sensors":{"battery":83},"stats":{"uptime":"82350","frame_cnt":3},"vendorName":"Aruba"}]};
for (const beacon of newData.devices[0].beacons) {
    console.log(beacon);
}

Image output

CodePudding user response:

you are close to the solution, but arr1 is an array, so it doesn't have a 'beacon' property. Use the following instead:

    const data = [
        {"mac":"f008d155b4fe","deviceClass":["iBeacon","eddystone"],"model":"iBeacon","lastSeen":"1637655820","bevent":{"event":"update"},"rssi":{"last":-84},"beacons":[{"ibeacon":{"uuid":"7$
        {"mac":"a0e6f8515eff","deviceClass":["arubaTag"],"firmware":{"bankA":"1.2-15"},"assetId":"0000-0000-0000","lastSeen":"1637655821","bevent":{"event":"update"},"rssi":{"last":-69},"sen$
  ];

  const arr1 = data.filter(d => d.mac == "f008d155b4fe");
  console.log('arr1', arr1);
  for (k of arr1) {
    console.log(k.beacons);
}

CodePudding user response:

arr1 is an Array it doesn't have beacons property. If you expect it to be the only one item with the mac you should use data.find instead of data.filter. Also do not use for-in loop to iterate over arrays. You can use for-of or .forEach instead.

const data = [
        {"mac":"f008d155b4fe","deviceClass":["iBeacon","eddystone"],"model":"iBeacon","lastSeen":"1637655820","bevent":{"event":"update"},"rssi":{"last":-84},"beacons":[{"ibeacon":{"uuid":"783a69707094d0863b4a9bf94e555241","major":816,"minor":6,"power":0},"eddystone":{"power":0}}],"sensors":{"temperatureC":18,"voltage":3.299999952316284},"stats":{"uptime":"710","adv_cnt":"24","frame_cnt":7}},
        {"mac":"a0e6f8515eff","deviceClass":["arubaTag"],"firmware":{"bankA":"1.2-15"},"assetId":"0000-0000-0000","lastSeen":"1637655821","bevent":{"event":"update"},"rssi":{"last":-69},"sensors":{"battery":83},"stats":{"uptime":"82350","frame_cnt":3},"vendorName":"Aruba"}
  ];
  
  // expecting only o1 
  const arr1 = data.find(d => d.mac == "f008d155b4fe");
  console.log('arr1', arr1);
  for (const beacon of arr1.beacons) { // use for-of or `forEach`
    console.log(beacon);
}
  • Related