Parsing stdout output

Hello

I’m trying to build a workflow to backup my ESXi homelab snapshots. My first step is to list the folders using an execute command node: ssh -i ./id_rsa [email protected] "ssh -i ./id_rsa [email protected] “find /vmfs/volumes/datastore1/backup/ -maxdepth 1 | tail -n +2”

I’m getting the following result in the stdout
/vmfs/volumes/datastore1/backup/opnsense /vmfs/volumes/datastore1/backup/debian /vmfs/volumes/datastore1/backup/keeweb

those are the directories returned by the find command. my purpose is to parse those directories in order to iterate over them and execute a tar command using another node.

my question is: what is the best way to iterate over this stdout output?

You can use a Function-Node with the following code:

return items[0].json.stdout.split('\n').map(folder => {
  return {
    json: {
      folder 
    }
  }
})

It will then add each of the folders as a different item. So all nodes after that will then automatically execute once for each of those items. So if you have another ExecuteCommand node afterward, will it execute once for each folder.

Hope that is helpful!

I have another question about parsing stdout output.

Ive tried a number of way to split the stdout data into a normal json output that I can iterate over but cant seem to get it to work. any ideas?

[
{
"code": 0,
"signal": null,
"stdout": "{ user: { aaa: 'aaa', bbb: '', ccc: 'ccc', ddd: 'ddd', eee: 'eee', fff: 'fff', ggg: 'ggg', hhh: 'hhh', iii: iii, jjj: false, kkk: 'kkk', lll: false, mmm: 0, nnn: false, ooo: 0, ppp: 0, qqq: 0, rrr: false, sss: false, ttt: false, uuu: '' }, vvv: { www: 0, xxx: 0, yyy: 0, zzz: 0, abab: 1, bcbc: 0 }, cdcd: [] }",
"stderr": ""
}
]

So you are getting a JavaScript object from your Execute Command node? First thing I’d check is whether your command can alternatively return valid JSON so you could just use JSON.parse() in a Function/Function Item node.

This is the output of the tool when you tell it to export in “json” and what shows in my SSH node when command is executed.

How can i parse this output from SSH?
I would like to process data from stdout item by item, which is in sdtout in array.

[
{
"code": 0,
"signal": null,
"stdout": "{"0":"https://test.pl/pl/used-cars/volvo/v60-cross-country/b5-b-awd-aut-t4cq5buk","1":"https://test.pl/pl/used-cars/volvo/v90-cross-country/d4-awd-aut-p5cj5gu5","2":"https://test.pl/pl/used-cars/volvo/v90-cross-country/d5-awd-pro-aut-p5clh373","3":"https://test.pl/pl/used-cars/volvo/xc40/xc-40-d3-scr-awd-inscription-aut-u7cqjmtp","4":"https://test.pl/pl/used-cars/volvo/v40-cross-country/t4-awd-drive-e-summum-rxcp74vs","5":"https://test.pl/pl/used-cars/volvo/v90/d5-awd-momentum-aut-qbclyznp","6":"https://test.pl/pl/used-cars/volvo/v60-cross-country/pro-b4-d-awd-aut-t4cphhh2","7":"https://test.pl/pl/used-cars/volvo/v90-cross-country/d4-awd-pro-rxcpvvds","8":"https://test.pl/pl/used-cars/volvo/s60/t8-awd-plug-in-hybrid-polestar-engineered-aut-jkckyb22","9":"https://test.pl/pl/used-cars/volvo/xc60/xc-60-d4-awd-inscription-nqcscknq","10":"https://test.pl/pl/used-cars/volvo/v60/d3-scr-momentum-aut-wdcp74v2","11":"https://test.pl/pl/used-cars/volvo/xc60/xc-60-d4-momentum-aut-p5cucmu2","12":"https://test.pl/pl/used-cars/volvo/xc60/xc-60-d4-scr-awd-r-design-mmcqp6v6","13":"https://test.pl/pl/used-cars/volvo/v60/d3-momentum-u7crd5st","14":"https://test.pl/pl/used-cars/volvo/xc40/xc-40-d3-awd-momentum-s1crp8yj"}",
"stderr": ""
}
] 

And this:

[
  {
    "code": 0,
    "signal": null,
    "stdout": "[\n  {\n    hrefs: [\n      'https://news.ycombinator.com/',\n      'https://news.ycombinator.com/news',\n      'https://news.ycombinator.com/newest',\n      'https://news.ycombinator.com/front',\n      'https://news.ycombinator.com/newcomments',\n      'https://news.ycombinator.com/ask',\n      'https://news.ycombinator.com/show',\n      'https://news.ycombinator.com/jobs',\n      'https://news.ycombinator.com/submit',\n      'https://news.ycombinator.com/login?goto=news',\n      'https://news.ycombinator.com/vote?id=32927360&how=up&goto=news',\n      'https://openai.com/blog/whisper/',\n      'https://news.ycombinator.com/from?site=openai.com',\n      'https://news.ycombinator.com/user?id=_just7_',\n      'https://news.ycombinator.com/item?id=32927360',\n      'https://news.ycombinator.com/hide?id=32927360&goto=news',\n      'https://news.ycombinator.com/item?id=32927360',\n      'https://news.ycombinator.com/vote?id=32929070&how=up&goto=news',\n      'https://www.datatilsynet.dk/english/google-analytics/use-of-google-analytics-for-web-analytics',\n      'https://news.ycombinator.com/from?site=datatilsynet.dk',\n      'https://news.ycombinator.com/user?id=ZacnyLos',\n      'https://news.ycombinator.com/item?id=32929070',\n      'https://news.ycombinator.com/hide?id=32929070&goto=news',\n      'https://news.ycombinator.com/item?id=32929070',\n      'https://news.ycombinator.com/vote?id=32926369&how=up&goto=news',\n      'https://frame.work/at/en/blog/introducing-the-framework-laptop-chromebook-edition',\n      'https://news.ycombinator.com/from?site=frame.work',\n      'https://news.ycombinator.com/user?id=artogahr',\n      'https://news.ycombinator.com/item?id=32926369',\n      'https://news.ycombinator.com/hide?id=32926369&goto=news',\n      'https://news.ycombinator.com/item?id=32926369',\n      'https://news.ycombinator.com/vote?id=32927156&how=up&goto=news',\n      'https://www.nasa.gov/feature/goddard/2022/new-webb-image-captures-clearest-view-of-neptune-s-rings-in-decades/',\n      'https://news.ycombinator.com/from?site=nasa.gov',\n      'https://news.ycombinator.com/user?id=pseudolus',\n      'https://news.ycombinator.com/item?id=32927156',\n      'https://news.ycombinator.com/hide?id=32927156&goto=news',\n      'https://news.ycombinator.com/item?id=32927156',\n      'https://news.ycombinator.com/vote?id=32925734&how=up&goto=news',\n      'https://fly.io/blog/introducing-litefs/',\n      'https://news.ycombinator.com/from?site=fly.io',\n      'https://news.ycombinator.com/user?id=danielskogly',\n      'https://news.ycombinator.com/item?id=32925734',\n      'https://news.ycombinator.com/hide?id=32925734&goto=news',\n      'https://news.ycombinator.com/item?id=32925734',\n      'https://news.ycombinator.com/vote?id=32926823&how=up&goto=news',\n      'https://gexp.no/blog/hacking-anything-with-gnu-guix.html',\n      'https://news.ycombinator.com/from?site=gexp.no',\n      'https://news.ycombinator.com/user?id=podiki',\n      'https://news.ycombinator.com/item?id=32926823',\n      'https://news.ycombinator.com/hide?id=32926823&goto=news',\n      'https://news.ycombinator.com/item?id=32926823',\n      'https://news.ycombinator.com/vote?id=32930605&how=up&goto=news',\n      'https://github.com/cassiozen/TDungeon',\n      'https://news.ycombinator.com/from?site=github.com/cassiozen',\n      'https://news.ycombinator.com/user?id=cacozen',\n      'https://news.ycombinator.com/item?id=32930605',\n      'https://news.ycombinator.com/hide?id=32930605&goto=news',\n      'https://news.ycombinator.com/item?id=32930605',\n      'https://news.ycombinator.com/vote?id=32929454&how=up&goto=news',\n      'https://fred.stlouisfed.org/graph/?g=TXcI',\n      'https://news.ycombinator.com/from?site=stlouisfed.org',\n      'https://news.ycombinator.com/user?id=cs702',\n      'https://news.ycombinator.com/item?id=32929454',\n      'https://news.ycombinator.com/hide?id=32929454&goto=news',\n      'https://news.ycombinator.com/item?id=32929454',\n      'https://news.ycombinator.com/vote?id=32926073&how=up&goto=news',\n      'https://www.theverge.com/2022/9/21/23364696/getty-images-ai-ban-generated-artwork-illustration-copyright',\n      'https://news.ycombinator.com/from?site=theverge.com',\n      'https://news.ycombinator.com/user?id=baptiste313',\n      'https://news.ycombinator.com/item?id=32926073',\n      'https://news.ycombinator.com/hide?id=32926073&goto=news',\n      'https://news.ycombinator.com/item?id=32926073',\n      'https://news.ycombinator.com/vote?id=32929126&how=up&goto=news',\n      'https://www.timothyrice.org/icecreamsalt/',\n      'https://news.ycombinator.com/from?site=timothyrice.org',\n      'https://news.ycombinator.com/user?id=iamkroot',\n      'https://news.ycombinator.com/item?id=32929126',\n      'https://news.ycombinator.com/hide?id=32929126&goto=news',\n      'https://news.ycombinator.com/item?id=32929126',\n      'https://news.ycombinator.com/vote?id=32928298&how=up&goto=news',\n      'https://blog.webpagetest.org/posts/will-html-content-make-site-faster/',\n      'https://news.ycombinator.com/from?site=webpagetest.org',\n      'https://news.ycombinator.com/user?id=tkadlec',\n      'https://news.ycombinator.com/item?id=32928298',\n      'https://news.ycombinator.com/hide?id=32928298&goto=news',\n      'https://news.ycombinator.com/item?id=32928298',\n      'https://news.ycombinator.com/vote?id=32923226&how=up&goto=news',\n      'https://www.pugetsystems.com/labs/articles/Effects-of-Grill-Patterns-on-Fan-Performance-Noise-107/',\n      'https://news.ycombinator.com/from?site=pugetsystems.com',\n      'https://news.ycombinator.com/user?id=yread',\n      'https://news.ycombinator.com/item?id=32923226',\n      'https://news.ycombinator.com/hide?id=32923226&goto=news',\n      'https://news.ycombinator.com/item?id=32923226',\n      'https://news.ycombinator.com/vote?id=32925760&how=up&goto=news',\n      'https://www.decipad.com/',\n      'https://news.ycombinator.com/from?site=decipad.com',\n      'https://news.ycombinator.com/user?id=g-camargo',\n      'https://news.ycombinator.com/item?id=32925760',\n      'https://news.ycombinator.com/hide?id=32925760&goto=news',\n      ... 130 more items\n    ]\n  }\n]",
    "stderr": ""
  }
]