node-red-contrib-papaparse
A Node-RED node that uses PapaParse for CSV parsing and unparsing with enhanced features and performance compared to the built-in CSV parser in Node-RED.
Installation
You can install this node directly from the Node-RED palette manager, or run the following command in your Node-RED user directory (typically ~/.node-red
):
npm install node-red-contrib-papaparse
Features
- Better Performance: PapaParse is optimized for speed and can handle large CSV files efficiently.
- More Options: Provides more configuration options than the built-in CSV parser.
- Dynamic Typing: Automatically converts numeric and boolean values to their respective types.
- Header Row Support: Can use the first row as field names.
- Error Handling: Provides detailed error information when parsing fails.
- Metadata: Includes metadata about the parsing process in the output.
- Bidirectional Conversion: Includes nodes for both CSV-to-JSON parsing and JSON-to-CSV unparsing.
- Smart Input Handling: The unparsing node can automatically detect and parse JSON strings, making it easy to use with file nodes.
Nodes
This package provides two nodes:
1. papaparse (CSV to JSON)
Converts CSV data to JSON format.
Usage
- Add the PapaParse node to your flow.
- Configure the node according to your CSV format.
- Send a message with the CSV data in the
msg.payload
property.
- The node will output a message with the parsed data in the
msg.payload
property and metadata in the msg.meta
property.
Configuration Options
- Header Row: If checked, the first row of the CSV will be treated as field names.
- Skip Empty Lines: If checked, empty lines will be skipped.
- Dynamic Typing: If checked, numeric and boolean values will be converted to their respective types.
- Delimiter: The character used to separate values. Leave empty for auto-detection.
- Newline: The character used for line breaks. Leave empty for auto-detection.
- Quote Char: The character used to quote fields.
- Escape Char: The character used to escape special characters.
- Comments: If checked, lines starting with the comment character will be skipped.
- Fast Mode: If checked, certain optimizations will be enabled at the expense of some features.
2. papaunparse (JSON to CSV)
Converts JSON data to CSV format.
Usage
- Add the PapaUnparse node to your flow.
- Configure the node according to your desired CSV format.
- Send a message with the JSON data in the
msg.payload
property. This can be:
- An array of objects or an array of arrays
- A JSON string containing an array (will be automatically parsed)
- The node will output a message with the CSV string in the
msg.payload
property.
Configuration Options
- Include Headers: If checked, field names will be included as the first row of the CSV.
- Skip Empty Lines: If checked, empty lines will be skipped.
- Delimiter: The character used to separate values. Leave empty for default (,).
- Newline: The character used for line breaks. Leave empty for default (\n).
- Quote Char: The character used to quote fields.
- Escape Char: The character used to escape special characters.
- Columns: Optional comma-separated list of columns to include in the output. If not specified, all columns will be included.
Example Flows
CSV to JSON Example
[
{
"id": "f6f2187d.f17ca8",
"type": "inject",
"z": "2b9467c1.b7c8c8",
"name": "Sample CSV",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "name,age,city\nJohn,30,New York\nJane,25,San Francisco\nBob,40,Chicago",
"payloadType": "str",
"x": 140,
"y": 120,
"wires": [
[
"a7f4d89d.cb4b58"
]
]
},
{
"id": "a7f4d89d.cb4b58",
"type": "papaparse",
"z": "2b9467c1.b7c8c8",
"name": "",
"header": true,
"skipEmptyLines": true,
"dynamicTyping": true,
"delimiter": "",
"newline": "",
"quoteChar": "\"",
"escapeChar": "\"",
"comments": false,
"fastMode": false,
"x": 320,
"y": 120,
"wires": [
[
"8f347a9a.41c7b8"
]
]
},
{
"id": "8f347a9a.41c7b8",
"type": "debug",
"z": "2b9467c1.b7c8c8",
"name": "Parsed CSV",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 120,
"wires": []
}
]
JSON to CSV Example
[
{
"id": "e5f2187d.f17ca8",
"type": "inject",
"z": "2b9467c1.b7c8c8",
"name": "Sample JSON",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"},{\"name\":\"Jane\",\"age\":25,\"city\":\"San Francisco\"},{\"name\":\"Bob\",\"age\":40,\"city\":\"Chicago\"}]",
"payloadType": "json",
"x": 140,
"y": 220,
"wires": [
[
"b7f4d89d.cb4b58"
]
]
},
{
"id": "b7f4d89d.cb4b58",
"type": "papaunparse",
"z": "2b9467c1.b7c8c8",
"name": "",
"header": true,
"skipEmptyLines": true,
"delimiter": "",
"newline": "",
"quoteChar": "\"",
"escapeChar": "\"",
"columns": "",
"x": 330,
"y": 220,
"wires": [
[
"9f347a9a.41c7b8"
]
]
},
{
"id": "9f347a9a.41c7b8",
"type": "debug",
"z": "2b9467c1.b7c8c8",
"name": "CSV Output",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 220,
"wires": []
}
]
Reading JSON File and Converting to CSV
[
{
"id": "c3f2187d.f17ca8",
"type": "file in",
"z": "2b9467c1.b7c8c8",
"name": "Read JSON File",
"filename": "data.json",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"x": 140,
"y": 320,
"wires": [
[
"d7f4d89d.cb4b58"
]
]
},
{
"id": "d7f4d89d.cb4b58",
"type": "papaunparse",
"z": "2b9467c1.b7c8c8",
"name": "",
"header": true,
"skipEmptyLines": true,
"delimiter": ",",
"newline": "",
"quoteChar": "\"",
"escapeChar": "\"",
"columns": "",
"x": 330,
"y": 320,
"wires": [
[
"e9347a9a.41c7b8"
]
]
},
{
"id": "e9347a9a.41c7b8",
"type": "file",
"z": "2b9467c1.b7c8c8",
"name": "Write CSV File",
"filename": "data.csv",
"appendNewline": false,
"createDir": false,
"overwriteFile": "true",
"encoding": "none",
"x": 500,
"y": 320,
"wires": []
}
]
License
ISC