| name | json-and-csv-data-transformation |
| description | Transform data between JSON, CSV, and other formats with filtering, mapping, and flattening. Use when: (1) Converting API responses to CSV, (2) Processing data pipelines, (3) Extracting specific fields, or (4) Flattening nested structures. |
JSON and CSV Data Transformation
Transform data between JSON, CSV, and other formats. Filter, map, flatten nested objects, and reshape data for analysis, reporting, and API integration.
When to use
- Use case 1: When the user asks to convert data between JSON and CSV formats
- Use case 2: When you need to filter, extract, or transform specific fields from data
- Use case 3: For flattening nested JSON structures into tabular format
- Use case 4: When processing API responses for analysis or reporting
Required tools / APIs
- jq — Command-line JSON processor (essential for JSON manipulation)
- csvkit — Suite of CSV tools (csvjson, csvcut, csvgrep, etc.)
- No external API required
Install options:
sudo apt-get install -y jq csvkit
brew install jq csvkit
Skills
json_to_csv
Convert JSON array to CSV format.
echo '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' | jq -r '(.[0] | keys_unsorted) as $keys | $keys, (map([.[ $keys[] ]]) | .[] | @csv)'
jq -r '(.[0] | keys_unsorted) as $keys | $keys, (map([.[ $keys[] ]]) | .[] | @csv)' data.json > output.csv
jq -r '.[] | [.id, .name, .email] | @csv' users.json
cat data.json | in2csv -f json > output.csv
Node.js:
function jsonToCSV(jsonArray) {
if (!Array.isArray(jsonArray) || jsonArray.length === 0) {
return '';
}
const headers = Object.keys(jsonArray[0]);
const escape = (val) => {
if (val === null || val === undefined) return '';
const str = String(val);
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
};
// Build CSV
const headerRow = headers.join(',');
const dataRows = jsonArray.map(obj =>
headers.map(header => escape(obj[header])).join(',')
);
return [headerRow, ...dataRows].join('\n');
}
// Usage
// const data = [
// { name: 'Alice', age: 30, city: 'New York' },
// { name: 'Bob', age: 25, city: 'San Francisco' }
// ];
// console.log(jsonToCSV(data));
csv_to_json
Convert CSV to JSON array.
csvjson data.csv
csvjson data.csv | jq '.'
csvjson --stream data.csv
csvjson input.csv > output.json
jq -Rsn '[inputs | split(",") | {name: .[0], age: .[1], city: .[2]}]' < data.csv
Node.js:
function csvToJSON(csvString) {
const lines = csvString.trim().split('\n');
if (lines.length < 2) return [];
const parseCSVValue = (val) => {
val = val.trim();
if (val.startsWith('"') && val.endsWith('"')) {
return val.slice(1, -1).replace(/""/g, '"');
}
return val;
};
const splitCSVLine = (line) => {
const result = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
inQuotes = !inQuotes;
current += char;
} else if (char === ',' && !inQuotes) {
result.push(parseCSVValue(current));
current = '';
} else {
current += char;
}
}
result.push(parseCSVValue(current));
return result;
};
const headers = splitCSVLine(lines[0]);
const data = lines.slice(1).map(line => {
const values = splitCSVLine(line);
const obj = {};
headers.forEach((header, i) => {
obj[header] = values[i] || '';
});
return obj;
});
return data;
}
filter_and_extract_json
Filter and extract specific fields from JSON.
jq '.[] | {name: .name, email: .email}' users.json
jq '.[] | select(.age > 25)' users.json
jq '[.[] | select(.active == true) | {id: .id, name: .name}]' data.json
jq '.[] | {name: .name, street: .address.street, city: .address.city}' data.json
jq '.[].name' users.json
jq '.[] | select(.age > 20 and .country == "USA")' users.json
jq '.[] | .price = (.price * 1.1)' products.json
Node.js:
function filterAndExtractJSON(data, options) {
const { filter, extract } = options;
let result = Array.isArray(data) ? data : [data];
if (filter) {
result = result.filter(filter);
}
if (extract) {
result = result.map(item => {
const extracted = {};
extract.forEach(field => {
const value = field.split('.').reduce((obj, key) => obj?.[key], item);
extracted[field] = value;
});
return extracted;
});
}
return result;
}
flatten_nested_json
Flatten nested JSON objects into flat structure.
jq '[.[] | {id: .id, name: .name, street: .address.street, city: .address.city, zip: .address.zip}]' users.json
jq '[.[] | to_entries | map({key: .key, value: .value}) | from_entries]' data.json
jq 'recurse | select(type != "object" and type != "array")' complex.json
Node.js:
function flattenJSON(obj, prefix = '', separator = '.') {
const flattened = {};
for (const key in obj) {
const value = obj[key];
const newKey = prefix ? `${prefix}${separator}${key}` : key;
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
Object.assign(flattened, flattenJSON(value, newKey, separator));
} else if (Array.isArray(value)) {
flattened[newKey] = JSON.stringify(value);
} else {
flattened[newKey] = value;
}
}
return flattened;
}
transform_csv_data
Transform and manipulate CSV data.
csvcut -c name,email,age users.csv
csvgrep -c age -r "^[3-9][0-9]$" users.csv
csvsort -c age -r users.csv
csvcut -c name,email users.csv | uniq
csvgrep -c country -m "USA" users.csv | csvcut -c name,age | csvsort -c age
awk -F',' 'BEGIN{OFS=","} NR==1{print $0,"total"} NR>1{print $0,$2*$3}' data.csv
csvjoin -c id users.csv orders.csv
Node.js:
function transformCSV(csvData, transformations) {
const { selectColumns, filterRows, sortBy } = transformations;
const data = csvToJSON(csvData);
let result = data;
if (filterRows) {
result = result.filter(filterRows);
}
if (selectColumns) {
result = result.map(row => {
const selected = {};
selectColumns.forEach(col => {
selected[col] = row[col];
});
return selected;
});
}
if (sortBy) {
const { column, reverse } = sortBy;
result.sort((a, b) => {
const aVal = a[column];
const bVal = b[column];
const comparison = aVal > bVal ? 1 : aVal < bVal ? -1 : 0;
return reverse ? -comparison : comparison;
});
}
return jsonToCSV(result);
}
aggregate_and_group_json
Aggregate and group JSON data (similar to SQL GROUP BY).
jq 'group_by(.country) | map({country: .[0].country, count: length})' users.json
jq 'group_by(.category) | map({category: .[0].category, total: map(.price) | add})' products.json
jq 'group_by(.department) | map({department: .[0].department, avg_salary: (map(.salary) | add / length)})' employees.json
jq 'group_by(.region) | map({
region: .[0].region,
count: length,
total_sales: map(.sales) | add,
avg_sales: (map(.sales) | add / length)
})' sales.json
Node.js:
function groupAndAggregate(data, groupBy, aggregations) {
const grouped = {};
data.forEach(item => {
const key = item[groupBy];
if (!grouped[key]) grouped[key] = [];
grouped[key].push(item);
});
return Object.entries(grouped).map(([key, items]) => {
const result = { [groupBy]: key };
aggregations.forEach(agg => {
if (agg.type === 'count') {
result[agg.name] = items.length;
} else if (agg.type === 'sum') {
result[agg.name] = items.reduce((sum, item) => sum + (item[agg.field] || 0), 0);
} else if (agg.type === 'avg') {
const sum = items.reduce((s, item) => s + (item[agg.field] || 0), 0);
result[agg.name] = items.length > 0 ? sum / items.length : 0;
} else if (agg.type === 'min') {
result[agg.name] = Math.min(...items.map(item => item[agg.field] || Infinity));
} else if (agg.type === 'max') {
result[agg.name] = Math.max(...items.map(item => item[agg.field] || -Infinity));
}
});
return result;
});
}
Rate limits / Best practices
- ✅ Stream large files — Use jq with
-c flag and process line by line for large datasets
- ✅ Validate data — Check JSON/CSV format before transformation
- ✅ Handle missing fields — Use default values for null/undefined fields
- ✅ Memory management — For files >100MB, use streaming parsers
- ✅ Type conversion — Be aware of number/string conversions in CSV
- ✅ Preserve data types — JSON maintains types, CSV converts everything to strings
- ⚠️ Character encoding — Ensure UTF-8 encoding for international characters
- ⚠️ Quote escaping — Properly escape quotes in CSV values
Agent prompt
You have JSON and CSV data transformation capability. When a user asks to transform data:
1. Identify the input format:
- JSON: Look for {...} or [...]
- CSV: Look for comma-separated values with headers
2. For JSON to CSV:
- Use jq with @csv filter: `jq -r '... | @csv'`
- Or csvkit: `in2csv -f json`
- Node.js: Convert array of objects to CSV string
3. For CSV to JSON:
- Use csvjson from csvkit: `csvjson file.csv`
- Node.js: Parse CSV headers and data rows into objects
4. For filtering/extracting:
- Use jq select(): `jq '.[] | select(.age > 25)'`
- Use csvkit csvgrep: `csvgrep -c column -m value`
- Node.js: Use Array.filter() and map()
5. For flattening:
- Flatten nested JSON objects into dot notation
- Convert nested structures to tabular format
- Handle arrays by stringifying or creating separate rows
6. For aggregation:
- Use jq group_by(): `jq 'group_by(.field) | map({...})'`
- CSV: Convert to JSON, aggregate, convert back
- Node.js: Implement grouping and aggregation functions
Always:
- Preserve data integrity (no data loss)
- Handle edge cases (empty values, special characters)
- Validate output format matches expected structure
- For large files (>100MB), recommend streaming approaches
Troubleshooting
Error: "parse error: Invalid numeric literal"
- Symptom: jq fails to parse JSON
- Solution: Validate JSON format with
jq empty file.json, fix syntax errors
CSV columns not aligned:
- Symptom: Data appears in wrong columns after transformation
- Solution: Check for unescaped commas in data, ensure quotes are properly escaped
Empty output from jq:
- Symptom: jq returns no results
- Solution: Check filter expression syntax, verify data structure matches filter
Special characters broken in CSV:
- Symptom: Non-ASCII characters appear garbled
- Solution: Ensure UTF-8 encoding:
iconv -f UTF-8 -t UTF-8 file.csv
Memory error with large files:
- Symptom: Process runs out of memory
- Solution: Use streaming mode:
jq -c or Node.js streams for line-by-line processing
JSON doesn't convert to flat CSV:
- Symptom: Nested objects create complex CSV structure
- Solution: Flatten JSON first before converting to CSV
See also