nodered teaser.png

Export JSON to CSV format using Node-RED in ctrlX OS

Community Manager
Community Manager

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).

Steps to perform
  1. Open your ctrlX OS console in the browser. You will be directed to the homepage. There, click on Manage app data.
  2. In the Manage app data page click on the Node-RED configuration in the Content table.
     Manage app data screenManage app data screen 
  3. 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.

    Node-RED configuration directoryNode-RED configuration directory 

    Sample json fileSample json file

  4. 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. 
    1. Common > inject node
    2. Storage > read file
    3. Parser > JSON
    4. Parser > CSV
    5. Storage > write File
    6. Common > debug (Optional- to test the output of the flow in debugger)
       Complete flow of Node-REDComplete flow of Node-RED
  5. 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.
  6. In the JSON node, set Action to Always convert to Javascript Object to properly parse your JSON file. 
  7. 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.
  8. 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.
  9. 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.
Related Information
Editorial work by: KatjaX
Must Read