How to use the json2csv.parseAsync function in json2csv

To help you get started, we’ve selected a few json2csv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / json2csv / json2csv-tests.ts View on Github external
const input = createReadStream('/path/to/input', { encoding: 'utf8' });
const output = createWriteStream('path/to/output', { encoding: 'utf8' });

asyncParser.fromInput(input).promise()
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

asyncParser.fromInput(input).toOutput(output);

// Test convenience method "parseAsync" with object input
parseAsync(data, opts)
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

// Test convenience method "parseAsync" with stream input
parseAsync(input, opts)
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

/********************
 * Internal Methods *
 ********************/
class ParserExt extends Parser {
    constructor() {
        super();
        // Parser methods
        obj = this.preprocessData({});
        obj = this.preprocessData({ str: '', num: 1, obj: {} });
        obj = this.preprocessData([]);
        s = this.processData([]);

        // JSON2CSVBase methods
github DefinitelyTyped / DefinitelyTyped / types / json2csv / json2csv-tests.ts View on Github external
.on('error', err => console.log(err));

asyncParser.input.push(data); // This data might come from an HTTP request, etc.
asyncParser.input.push(null); // Sending `null` to a stream signal that no more data is expected and ends it.

const input = createReadStream('/path/to/input', { encoding: 'utf8' });
const output = createWriteStream('path/to/output', { encoding: 'utf8' });

asyncParser.fromInput(input).promise()
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

asyncParser.fromInput(input).toOutput(output);

// Test convenience method "parseAsync" with object input
parseAsync(data, opts)
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

// Test convenience method "parseAsync" with stream input
parseAsync(input, opts)
    .then(csv => console.log(csv))
    .catch(err => console.error(err));

/********************
 * Internal Methods *
 ********************/
class ParserExt extends Parser {
    constructor() {
        super();
        // Parser methods
        obj = this.preprocessData({});
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formSubmissionResolvers / exportFormSubmissions.js View on Github external
// Build rows.
    for (let i = 0; i < submissions.length; i++) {
        let submissionData = submissions[i].data;
        const row = {};
        Object.keys(fields).map(fieldId => {
            if (fieldId in submissionData) {
                row[fields[fieldId]] = submissionData[fieldId];
            } else {
                row[fields[fieldId]] = "N/A";
            }
        });
        rows.push(row);
    }

    // Save CSV file and return its URL to the client.
    const csv = await parseAsync(rows, { fields: Object.values(fields) });
    const buffer = Buffer.from(csv);
    const result = await uploadFile({
        context,
        buffer,
        file: {
            size: buffer.length,
            name: "form_submissions_export.csv",
            type: "text/csv"
        }
    });
    return new Response(result);
};
github rolling-scopes / rsschool-app / server / src / routes / course / score.ts View on Github external
const courseId = ctx.params.courseId;
  const students = await getStudentsScore(courseId);
  const courseTasks = await getCourseTasks(courseId);

  const result = students.map(student => {
    return {
      githubId: student.githubId,
      name: student.name,
      locationName: student.locationName,
      countryName: countriesMap[citiesMap[student.locationName]] || 'Other',
      mentorGithubId: student.mentor ? (student.mentor as any).githubId : '',
      totalScore: student.totalScore,
      ...getTasksResults(student.taskResults, courseTasks),
    };
  });
  const csv = await parseAsync(result);
  setCsvResponse(ctx, OK, csv, 'score');
};

json2csv

Convert JSON to CSV

MIT
Latest version published 2 years ago

Package Health Score

76 / 100
Full package analysis