2022.2

Table Of Contents
l
Thefs-ops-dirnode(package:node-red-contrib-fs-ops)listsfilesinadirectory.
l
Thewatch-directorynodecapturesincomingfiles.Thisnodeispreferabletothestandard
watchnodeasthewatchnodemaytriggertheflowbeforeafileiscompletelywritten,whichcan
becomeproblematicwhenprocessinglargerinputfiles.
Reading a JSON file
InordertoloadaJSONfileyoucanusearead filenode.SettheFilenamepropertytothefullpath.
NotethatwhenNode-RED'sprojectfeatureisused,thepathisrelativetotheprojectlocationunder
NR'suserdirectory(%userprofile%/.node-red/).
Example: Whentriggered,astartupflowmustreadthemanifest.jsonfile.
l
Addaread filenodeaftertheinjectnodethattriggerstheflow,andconnecttheir
ports.
l
Double-clicktheread filenodetoviewitsproperties.
l
Pasteorenterthefullpathtomanifest.jsonintheFilenameproperty.For
example:C:\projects\project-a\resources\manifest.json,or,when
usingNode-RED'sprojectfeature:node-red\projects\project-a\mani-
fest.json.
l
YoucanchangetheNameofthenode,forexampleinto:Readmanifest.
l
ClickDone.
Parsing a JSON string
WhenaJSONfileisread,theresultisaJSONstring.Inordertoaccessitsdata,thatstringneedstobe
parsedtoitsJavaScriptObjectrepresentation.Node-REDprovidestheJSONnodetodothiscon-
version.
Theparseddataiswrittentothepayloadpropertyofthemsgobject,whichispassedontothenext
node.KeysintheJSONbecomepropertiesofthepayload.
Example
Afterreadingthemanifest.jsonfileusingaread filenode,astartupflowmustparsetheJSONstringin
ordertoaddthekeysandvaluestomsg.payload.Themanifest.jsonfilehasthefollowingcontent:
{
"email": "Laura from OL Acme <laura@ol-acme.com>",
"someApi": "http://localhost/myendpoint",
"workspace": "C:\\workspace",
"resources": [
"olsg-invoice.OL-template",
Page 186