Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Created December 28, 2023 17:35
Show Gist options
  • Save msrivastav13/c5a17a99d24e4263e30610b54d971e62 to your computer and use it in GitHub Desktop.
Save msrivastav13/c5a17a99d24e4263e30610b54d971e62 to your computer and use it in GitHub Desktop.
Converts SnowFakery generated test data to the JSON format for loading via Salesforce CLI
import csvtojson from 'csvtojson';
import fs from 'fs/promises';
// Define a reusable function for converting CSV to JSON
const convertCsvToJson = async (
csvFilePath,
jsonFilePath,
objectType,
idPrefix = ''
) => {
const jsonArray = await csvtojson().fromFile(csvFilePath);
const newArray = jsonArray.map((obj) => {
obj = {
attributes: { type: objectType, referenceId: objectType + obj.id },
...obj
};
if (idPrefix) {
obj.AccountId = `@${idPrefix}${obj.AccountId}`;
}
delete obj.id;
return obj;
});
const output = { records: newArray };
await fs.writeFile(jsonFilePath, JSON.stringify(output, null, 2));
console.log(`Conversion completed for ${objectType} Object!`);
};
// Specify the paths and object types
const csvAccountFilePath = '/home/codebuilder/dreamhouse-lwc/data/Account.csv';
const jsonAccountFilePath =
'/home/codebuilder/dreamhouse-lwc/data/Account.json';
const csvContactFilePath = '/home/codebuilder/dreamhouse-lwc/data/Contact.csv';
const jsonContactFilePath =
'/home/codebuilder/dreamhouse-lwc/data/Contact.json';
// Convert Account CSV to JSON
convertCsvToJson(csvAccountFilePath, jsonAccountFilePath, 'Account');
// Convert Contact CSV to JSON
convertCsvToJson(csvContactFilePath, jsonContactFilePath, 'Contact', 'Account');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment