Read the contents of a folder

#1

I have a local folder with uknown number of xml files. I want to take the xml files convert them to json and make a new folder with the json files. Is it possible with node - red. (I know how to convert xml to json)

0 Likes

#2

Each json will be a independent plan text if is the case you will have to writte a function to automatically create a different output file name for each one, set this as msg.filename and the json as payload and then pull this into a file writte node

0 Likes

#3

I did that and it worked .But how can i send an http post request and post all the created json files to a http endpoint.

0 Likes

#4

Take a look at the http request node

0 Likes

#5

can the http request payload be multiple json files?

0 Likes

#6

If you are loading the files into Node-RED to convert from xml to JSON the flow handles each flow separately, which is what I thought you were doing.

Otherwise look on flows.nodered.org for nodes that can handle multiple files

1 Like

#7

I found the solution using the node-red-contrib-fs-ops and node-node-red-contrib-fullsplitter. For those of you that have the same question just use the fs-ops-dir node to get the array of all the files contained in the folder and then use the fullsplitter node. The flow is not functional of course just use your own paths and type your code in the “give file path” function.

```[{"id":"4d82c25e.1e4b3c","type":"fs-ops-dir","z":"ca4f9243.31a82","name":"JSON Folder","path":"C:\\Users\\a.melandinos\\Documents\\JSONfiles","pathType":"str","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":306.88671112060547,"y":154.55859661102295,"wires":[["63fedd7b.9d56a4"]]},{"id":"778b18c5.f71288","type":"switch","z":"ca4f9243.31a82","name":"","property":"files","propertyType":"msg","rules":[{"t":"null"}],"checkall":"true","repair":false,"outputs":1,"x":532.8828926086426,"y":297.24225521087646,"wires":[["63fedd7b.9d56a4"]]},{"id":"991545d3.ac8778","type":"file in","z":"ca4f9243.31a82","name":"File to be posted","filename":"","format":"utf8","chunk":false,"sendError":false,"x":882.51953125,"y":62.69533157348633,"wires":[["c0c05a36.a5ed38"]]},{"id":"4d0d5aa1.a10aa4","type":"function","z":"ca4f9243.31a82","name":"give file path","func":"//Create the name of the file we need to retrieve in order to POST it\n//later.\nvar filepath =\"C:/Users/a.melandinos/Documents/JSONfiles/\";\nvar prefix = msg.array;\nvar prefixFinal = JSON.stringify(prefix);\n\nprefixFinal=prefixFinal.substr(1).slice(0, -1);\n\nvar finalPath  = filepath.concat(prefixFinal);\n\nfinalPath.replace(\"/\",\"\\ \");\nmsg.filename = finalPath;\n\nreturn msg;","outputs":1,"noerr":0,"x":739.5118560791016,"y":165.79689598083496,"wires":[["991545d3.ac8778"]]},{"id":"dd92c82e.c68d68","type":"comment","z":"ca4f9243.31a82","name":"Looping in the folder till no file left.","info":"This node takes an array as an input and passes\neach element of the array individually as an output.\n\nThe output is the name of the file.\nFor example \"file1.json\" .","x":539.8907012939453,"y":122.59766006469727,"wires":[]},{"id":"63fedd7b.9d56a4","type":"fullsplitter","z":"ca4f9243.31a82","name":"Split json files","property":"files","x":527.8984909057617,"y":168.94145011901855,"wires":[["4d0d5aa1.a10aa4"],["778b18c5.f71288"]]}]```
0 Likes

#8

Use three backticks before and after code snippets to format correctly so they ca be imported directly into Node-RED. Thanks

0 Likes

#9

i am new didnt know that thanks for the advice sir

0 Likes

#10

… and you can edit your old posts to add them if you like :wink:

0 Likes