Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(csv): plain-language step forms, and JSON to CSV keeps every column (#15821)

Talal Jaber641817c9887d
diff --git a/packages/pieces/core/csv/src/lib/actions/convert-csv-to-json.ts b/packages/pieces/core/csv/src/lib/actions/convert-csv-to-json.ts
index 78b5defaccd..1fc0b41a2db 100644
--- a/packages/pieces/core/csv/src/lib/actions/convert-csv-to-json.ts
+++ b/packages/pieces/core/csv/src/lib/actions/convert-csv-to-json.ts
@@ -8,22 +8,25 @@ export const csvToJsonAction = createAction({
classification: 'READ',
displayName: 'Convert CSV to JSON',
description:
- 'This function reads a CSV string and converts it into JSON array format.',
+ 'Turns CSV text into a list of rows you can loop over or filter.',
aiMetadata: { description: 'Parses a CSV or tab-separated text string into a JSON array, treating the first row either as column headers or as plain data depending on the header option. Use this to make CSV text queryable before looping or filtering rows; use Convert JSON to CSV for the reverse direction, and run Convert Excel to CSV first when the source is an .xlsx/.xls file. The chosen delimiter (comma or tab) must match the actual data or columns will not split; read-only and idempotent.', idempotent: true },
props: {
csv_text: Property.LongText({
displayName: 'CSV Text',
+ description: 'Paste the CSV, or map text from an earlier step.',
+ placeholder: 'name,email,city',
defaultValue: '',
required: true,
}),
has_headers: Property.Checkbox({
- displayName: 'Does the CSV have headers?',
+ displayName: 'Has Header Row',
+ description: 'On: rows become objects keyed by column name. Off: lists of values.',
defaultValue: false,
- required: true,
+ required: false,
}),
delimiter_type: Property.StaticDropdown({
- displayName: 'Delimiter Type',
- description: 'Select the delimiter type for the CSV text.',
+ displayName: 'Delimiter',
+ description: 'The character that separates columns in your CSV.',
defaultValue: '',
required: true,
options: {
@@ -37,12 +40,10 @@ export const csvToJsonAction = createAction({
async run(context) {
const { csv_text, has_headers, delimiter_type } = context.propsValue;
if (!isString(csv_text)) {
- throw new Error(JSON.stringify({
- message: 'The input should be a string.',
- }))
+ throw new Error('The input should be a string.')
}
- const records = parse(csv_text,{delimiter: delimiter_type,columns: has_headers ? true : false});
+ const records = parse(csv_text,{delimiter: delimiter_type,columns: has_headers ? true : false,bom: true});
return records;
},
});
\ No newline at end of file
diff --git a/packages/pieces/core/csv/src/lib/actions/convert-excel-to-csv.ts b/packages/pieces/core/csv/src/lib/actions/convert-excel-to-csv.ts
index 987178c66cf..b0a09f5981e 100644
--- a/packages/pieces/core/csv/src/lib/actions/convert-excel-to-csv.ts
+++ b/packages/pieces/core/csv/src/lib/actions/convert-excel-to-csv.ts
@@ -7,7 +7,7 @@ export const excelToCsvAction = createAction({
name: 'convert_excel_to_csv',
classification: 'READ',
displayName: 'Convert Excel to CSV',
- description: 'Converts an Excel file (.xlsx or .xls) into CSV text.',
+ description: 'Turns one sheet of an Excel file (.xlsx or .xls) into CSV text.',
aiMetadata: { description: 'Reads a binary Excel workbook (.xlsx or .xls) and converts a single sheet to delimited CSV text (comma, tab, or semicolon), selecting either a named sheet or the first sheet when no name is given. Pick this when the source is a spreadsheet file; use Convert CSV to JSON when you already have CSV text. Requires a real Excel file whose signature is validated, so HTML or PDF content is rejected, and any sheet name given must exist in the workbook; read-only and idempotent.', idempotent: true },
props: {
file: Property.File({
@@ -18,18 +18,19 @@ export const excelToCsvAction = createAction({
sheet_name: Property.ShortText({
displayName: 'Sheet Name',
description: 'Name of the sheet to convert. Leave blank to use the first sheet.',
+ placeholder: 'Sheet1',
required: false,
}),
delimiter_type: Property.StaticDropdown({
displayName: 'Delimiter',
- description: 'Character used to separate values in the output CSV.',
+ description: 'The character that separates columns in the CSV output.',
defaultValue: ',',
required: true,
options: {
options: [
- { label: 'Comma (,)', value: ',' },
+ { label: 'Comma', value: ',' },
{ label: 'Tab', value: '\t' },
- { label: 'Semicolon (;)', value: ';' },
+ { label: 'Semicolon', value: ';' },
],
},
}),
diff --git a/packages/pieces/core/csv/src/lib/actions/convert-json-to-csv.ts b/packages/pieces/core/csv/src/lib/actions/convert-json-to-csv.ts
index 7457ddf99e9..9b25e2c85ec 100644
--- a/packages/pieces/core/csv/src/lib/actions/convert-json-to-csv.ts
+++ b/packages/pieces/core/csv/src/lib/actions/convert-json-to-csv.ts
@@ -1,23 +1,21 @@
-import { createAction, Property } from '@activepieces/pieces-framework';
+import { createAction, MarkdownVariant, Property } from '@activepieces/pieces-framework';
import { flatten } from 'safe-flat';
import { stringify } from "csv-stringify/sync";
import { jsonToCsvActionOutputSchema } from '../output-schemas';
-const markdown = `
-**Notes**:
-* The input should be a JSON array.
-* The JSON object will be flattened If nested and the keys will be used as headers.
-`
+const markdown = 'Each object becomes a row and each key a column; missing keys stay blank. Nested fields become columns named with dots, like `address.city`.';
+
export const jsonToCsvAction = createAction({
audience: 'both',
name: 'convert_json_to_csv',
classification: 'READ',
displayName: 'Convert JSON to CSV',
- description: 'This function reads a JSON array and converts it into CSV format.',
+ description: 'Turns a list of JSON objects into CSV text, one row per object.',
aiMetadata: { description: 'Serializes a JSON array into delimited CSV text (comma or tab), flattening nested objects so dotted key paths become the column headers. Use this when preparing tabular data for a file, export, or attachment; use Convert CSV to JSON for the reverse direction. The input must be a JSON array of rows, not a single object, and the header row is always emitted; pure transformation, read-only and idempotent.', idempotent: true },
props: {
markdown: Property.MarkDown({
value: markdown,
+ variant: MarkdownVariant.INFO,
}),
json_array: Property.Json({
displayName: 'JSON Array',
@@ -40,12 +38,12 @@ export const jsonToCsvAction = createAction({
}
],
description:
- 'Provide a JSON array to convert to CSV format.',
+ 'A list of objects, one per row. Map it from an earlier step.',
required: true,
}),
delimiter_type: Property.StaticDropdown({
- displayName: 'Delimiter Type',
- description: 'Select the delimiter type for the CSV file.',
+ displayName: 'Delimiter',
+ description: 'The character that separates columns in the CSV output.',
defaultValue: ',',
required: true,
options: {
@@ -60,15 +58,27 @@ export const jsonToCsvAction = createAction({
async run(context) {
const { json_array, delimiter_type } = context.propsValue;
if (!Array.isArray(json_array)) {
- throw new Error(JSON.stringify({
- message: 'The input should be a JSON array.',
- }))
+ throw new Error('The input should be a JSON array.');
}
const flattened = json_array.map((item) => flatten(item) as Record<string, string>);
+ if (flattened.length === 0) {
+ return '';
+ }
return stringify(flattened, {
header: true,
delimiter: delimiter_type,
+ columns: collectColumns(flattened),
});
},
});
+
+function collectColumns(rows: Record<string, string>[]): string[] {
+ const columns = new Set<string>();
+ for (const row of rows) {
+ for (const key of Object.keys(row)) {
+ columns.add(key);
+ }
+ }
+ return Array.from(columns);
+}
diff --git a/packages/pieces/core/csv/test/csv.test.ts b/packages/pieces/core/csv/test/csv.test.ts
index 30dd87cda1e..7e0030f648e 100644
--- a/packages/pieces/core/csv/test/csv.test.ts
+++ b/packages/pieces/core/csv/test/csv.test.ts
@@ -65,6 +65,22 @@ describe('csvToJsonAction', () => {
});
await expect(csvToJsonAction.run(ctx)).rejects.toThrow();
});
+
+ test('strips a leading byte-order mark from the first header', async () => {
+ const csvText = '\uFEFFname,age\nAlice,30';
+ const ctx = createMockActionContext({
+ propsValue: { csv_text: csvText, has_headers: true, delimiter_type: ',' },
+ });
+ const result = await csvToJsonAction.run(ctx);
+ expect(result).toEqual([{ name: 'Alice', age: '30' }]);
+ });
+
+ test('throws a readable message if input is not a string', async () => {
+ const ctx = createMockActionContext({
+ propsValue: { csv_text: 123 as unknown as string, has_headers: true, delimiter_type: ',' },
+ });
+ await expect(csvToJsonAction.run(ctx)).rejects.toThrow(/^The input should be a string\.$/);
+ });
});
describe('jsonToCsvAction', () => {
@@ -112,6 +128,31 @@ describe('jsonToCsvAction', () => {
});
await expect(jsonToCsvAction.run(ctx)).rejects.toThrow();
});
+
+ test('includes columns from every row, not only the first', async () => {
+ const jsonArray = [{ a: 1 }, { a: 2, b: 3 }];
+ const ctx = createMockActionContext({
+ propsValue: { markdown: '', json_array: jsonArray, delimiter_type: ',' },
+ });
+ const result = await jsonToCsvAction.run(ctx);
+ expect(result).toBe('a,b\n1,\n2,3\n');
+ });
+
+ test('returns an empty string for an empty array', async () => {
+ const ctx = createMockActionContext({
+ propsValue: { markdown: '', json_array: [], delimiter_type: ',' },
+ });
+ const result = await jsonToCsvAction.run(ctx);
+ expect(result).toBe('');
+ });
+
+ test('throws a readable message if input is not an array', async () => {
+ const ctx = createMockActionContext({
+ propsValue: { markdown: '', json_array: { not: 'an array' } as unknown as unknown[], delimiter_type: ',' },
+ });
+ await expect(jsonToCsvAction.run(ctx)).rejects.toThrow(/^The input should be a JSON array\.$/);
+ await expect(jsonToCsvAction.run(ctx)).rejects.not.toThrow(/^\{/);
+ });
});
describe('excelToCsvAction', () => {