We help IT Professionals succeed at work.

Unable to extract Json data

Rama Tito
Rama Tito asked
on
187 Views
Last Modified: 2018-12-08
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

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

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

Open in new window

Rama TitoProgrammer

Author

Commented:
Hi, '0' is undefined

node2.png
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
could you post it ?
if needed use : JSON.stringify(json) in the console and copy past the string you get
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

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

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
CERTIFIED EXPERT
Distinguished Expert 2019

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

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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions