Reputation: 41
When I execute tasklist on child_process the stdout retuns the processes in Unicode string format which I cannot easily query.
This is my code;
var exec = require('child_process').exec;
...
exec('tasklist', function(err, stdout, stderr) {
...
});
This is a part of stdout;
\r\nImage Name PID Session Name Session# Mem Usage\r\n========================= ======== ================ =========== ============\r\nSystem Idle Process 0 Services 0 8 K\r\nSystem 4 Services 0 3,900 K\r\nRegistry 148 Services 0 57,232 K\r\nsmss.exe 636 Services 0 444 K\r\ncsrss.exe 820 Services 0 3,604 K\r\nwininit.exe 716 Services 0 2,824 K\r\nservices.exe 8 Services 0 9,180 K\r\nlsass.exe...
How can I get the stdout in JSON format?
I tried several methods to convert from Unicode to JSON but could not find a simple way.
Upvotes: 0
Views: 308
Reputation:
parse the string by splitting it into lines, then further splitting each line into an array of values. You can then use this array to construct a JSON object
example:
var exec = require('child_process').exec;
exec('tasklist', function(err, stdout, stderr) {
var lines = stdout.split("\n");
var json = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (line === "") continue;
var values = line.split(/\s+/);
json.push({
"imageName": values[0],
"pid": values[1],
"sessionName": values[2],
"sessionNumber": values[3],
"memUsage": values[4]
});
}
console.log(json);
});
Upvotes: 0