Unable to extract Json data

Rama Tito
Rama Tito used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

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

Open in new window

Rama TitoProgrammer

Author

Commented:
Hi, '0' is undefined

node2.png
leakim971Multitechnician
Top Expert 2014

Commented:
could you post it ?
if needed use : JSON.stringify(json) in the console and copy past the string you get
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
or simply type "msg" and paste the string you get
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

leakim971Multitechnician
Top Expert 2014

Commented:
@Julian you have an extra dot here :
var MAC = json.["ne
Most Valuable Expert 2017
Distinguished Expert 2018

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

Open in new window

Rama TitoProgrammer

Author

Commented:
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

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
That's a different problem. You are not extracting your data correctly

I am assuming the error refers to line 1?

What is it that produces the console dump of the data in the screen grab of your opening post?

There is obviously code missing from this - what comes before line 1 of the code above?
Multitechnician
Top Expert 2014
Commented:
RESULTS : msg.payload:undefined
one more time, could you just post what you get in the console when you type "msg" ? thanks.
Rama TitoProgrammer

Author

Commented:
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.
Rama TitoProgrammer

Author

Commented:
Thank you for your assistant. Without your feedback i am unable to get solution.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial