Content
For every work using JSON objects or file type, we seldom come across scenarios when we have to convert it into a CSV (Comma Separated Values) format. This is very much possible on ctrlX CORE when working with Node-RED.
Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click. Learn more about Node-RED (external page).
Requirements
- Running ctrlX OS version 2.x, e.g. on a ctrlX CORE
- ctrlX OS - Node-RED installed
Steps to perform
- Open your ctrlX OS console in the browser. You will be directed to the homepage. There, click on Manage app data.
- In the Manage app data page click on the Node-RED configuration in the Content table.
- Once you have accessed the Node-RED configurations folder, you will observe many files that are associated to the Node-RED installed in your ctrlX OS. You can add a folder/directory here which will be used in the Node-RED. This data can be easily imported from your local machine or can even be created using the Create File option under Creates Directory or file option at the top right corner with as shown in the image below. For this demonstration we have created a daten folder which will have a sample.json file having a dummy json object.
- Once we are setup with our data, we will open the Node-RED flow editor. In the editor we use the following nodes as shown in the image below.
- Common > inject node
- Storage > read file
- Parser > JSON
- Parser > CSV
- Storage > write File
- Common > debug (Optional- to test the output of the flow in debugger)
- We use the inject as a trigger for the flow using the timestamp. In the read file node we use the file location "solutions/activeConfiguration/node-RED/daten/sample.json" to read the data. Please change the directory name as per your requirement.
- In the JSON node, set Action to Always convert to Javascript Object to properly parse your JSON file.
- Next we connect JSON node to the CSV node. In the CSV node you can specify the column names in the input box of Columns field. You can also leave it blank for the node to parse the column names automatically.
- We are now ready to connect our final node, i.e. the write file node which will save the CSV into the required folder. In the demonstration we have saved the file in the same folder as the input data. You can select the "Create directory if it doesn't exist?" option so as to create a directory in case not already created and is not same as the input data directory.
Note: Add the full path to your file to be saved along with the destination file name and extension.
Voila! We are set to test our flow. - Once we deploy the flow and click on the trigger node of the flow, the JSON file is fetched >> parsed by the JSON node >> converted into CSV >> stored in the destination folder.