Link to home
Start Free TrialLog in
Avatar of Rama Tito
Rama TitoFlag 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


User generated image
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Open in new window

Avatar of Rama Tito

ASKER

Hi, '0' is undefined

User generated image
could you post it ?
if needed use : JSON.stringify(json) in the console and copy past the string you get
or simply type "msg" and paste the string you get
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

@Julian you have an extra dot here :
var MAC = json.["ne
@Leakim, thanks - copy and paste hangover from the line below
Corrected
var MAC = json["networkInterfaces"]["Wi-Fi"1].mac;

Open in new window

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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thank you for your assistant. Without your feedback i am unable to get solution.
You are welcome.