Extract data from array json format.

Rama Tito
Rama Tito used Ask the Experts™
on
I am extracting all data from mysql using Node-Red. Its give set of data in json format but with opening and closing quote to indicate as string format. I did try many ways to pull out particular line.

" [   {"ID":1,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:28:24.000Z","IdRef":1} ,
       {"ID":2,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:29:53.000Z","IdRef":1} ,
       {"ID":3,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:29:59.000Z","IdRef":1} ,
       {"ID":4,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:15.000Z","IdRef":1} ,
       {"ID":5,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:16.000Z","IdRef":1} ,
       {"ID":6,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:16.000Z","IdRef":1} ,
       {"ID":7,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:17.000Z","IdRef":1} ,
       {"ID":8,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:17.000Z","IdRef":1}   ] "

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

var Device = json[1].Device_ID;
msg.payload = Device;
returen msg;

Open in new window


Error message as follows :

SELECT *FROM mytab : msg.payload : undefined
undefined
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
it's DeviceId and not Device_ID here :
var Device = json[1].Device_ID;

Open in new window


DeviceId
leakim971Multitechnician
Top Expert 2014

Commented:
a good tools : http://jsonviewer.stack.hu/

[{"ID":1,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:28:24.000Z","IdRef":1},{"ID":2,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:29:53.000Z","IdRef":1},{"ID":3,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:29:59.000Z","IdRef":1},{"ID":4,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:15.000Z","IdRef":1},{"ID":5,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:16.000Z","IdRef":1},{"ID":6,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:16.000Z","IdRef":1},{"ID":7,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:17.000Z","IdRef":1},{"ID":8,"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-16T13:39:17.000Z","IdRef":1}]

Open in new window


JSON Viewer
Rama TitoProgrammer

Author

Commented:
Thank you , Do appreciate most

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