I am using the npm module interfaces
to return network adapter details. It returns what looks like JSON formatted data. I would like to pull specific properties like "address", but am having trouble parsing the data.
Data
{
'Ethernet 1': [
{
address: 'ffff::ffff:ffff:ff:ffff',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
internal: false,
cidr: 'ffff::ffff:ffff:ff:ffff/64',
scopeid: 7
},
{
address: '10.0.0.1',
netmask: '255.255.255.192',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: false,
cidr: '10.0.0.1/26'
}
],
'Loopback Pseudo-Interface 1': [
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '::1/128',
scopeid: 0
},
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '127.0.0.1/8'
}
]
}
I have tried to stringify/parse the data into an array, but I am not sure how to identify the first object without using the specific name ('Ethernet 1').
My Code:
const interfaces = require('interfaces');
const netinterfaces = JSON.stringify(interfaces());
const results = JSON.parse(netinterfaces);
app.get('/nettest', (req, res) => {
const {results: {adapter:[{address}]}} =results;
console.log(address);
console.log(results);
res.send(results);
})
Error:
TypeError: Cannot read properties of undefined (reading 'adapter')