Avatar of Rama Tito
Rama Tito
Flag for Malaysia asked on

Unable to extract Json data

Hi, I am unable to extract the mac id info from the attached picture

var obj = msg.payload;
var json = JSON.parse(obj);


var MAC = json[0][1].mac;

msg.payload = MAC;

return msg;

Open in new window


mac.png
Node.js* node-redJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
leakim971

var MAC = json["Wi-Fi"][0].mac;

Open in new window

Rama Tito

ASKER
Hi, '0' is undefined

node2.png
leakim971

could you post it ?
if needed use : JSON.stringify(json) in the console and copy past the string you get
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
leakim971

or simply type "msg" and paste the string you get
Julian Hansen

I don't understand line 2
var json = JSON.parse(obj);

Open in new window

obj is set by accessing the property payload on msg - which indicates msg is already an object - so why parse it again.

Then you are missing a networkInterfaces property in your tree
var MAC = json.["networkInterfaces"]["Wi-Fi"1].mac;

Open in new window

OR
var MAC = json.networkInterfaces["Wi-Fi"1].mac;

Open in new window

leakim971

@Julian you have an extra dot here :
var MAC = json.["ne
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

@Leakim, thanks - copy and paste hangover from the line below
Corrected
var MAC = json["networkInterfaces"]["Wi-Fi"1].mac;

Open in new window

Rama Tito

ASKER
var obj = msg.payload;
var json = JSON.parse(obj);


var MAC = json["networkInterfaces"]["Wi-Fi"].mac;

msg.payload = MAC;
return msg;

RESULTS : msg.payload:undefined

Open in new window

SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rama Tito

ASKER
Hi Experts,
The payload: object in this format: -
           -> networkInterfaces
                      ->Wi-Fi
                                ->0: object
                                          ->mac:"7c:8b:ca:0d:2b:eb"
                                ->1.object
var obj = msg.payload;
var json = JSON.parse(obj);


var MAC = json["networkInterfaces"]["Wi-Fi"][0].mac;

msg.payload = MAC;
return msg;

Open in new window


i did added [0] and manage to extract data.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Rama Tito

ASKER
Thank you for your assistant. Without your feedback i am unable to get solution.
Julian Hansen

You are welcome.