How to use json-2-csv - 9 common examples

To help you get started, we’ve selected a few json-2-csv 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 frdmn / rocketchat-export-userlist / export-userlist.js View on Github external
function convertToCsvAndWriteToFile(users, cb) {
    // Convert to CSV
    converter.json2csv(users,function(err, csv){
        if(err) {
            return cb(err);
        }

        fs.writeFile(config.exportfile + ".csv", csv, function(err) {
            if(err) {
                return cb(err);
            }

            return cb(true);
        }); 
    }, {
        // Do not check for key differences in each user object
        checkSchemaDifferences: false,
        // Make sure to wrap CSV values in double quotes
        delimiter: {
github RandomAPI / Offline-RandomAPI / api / Generator.js View on Github external
time: this.times
      }
    };

    if (this.noInfo) delete json.info;
    if (this.hideuserinfo && !this.noInfo) delete json.info.user;
    if (this.sole) json.results = json.results[0];

    if (this.format === 'yaml') {
      cb(null, YAML.stringify(json, 4), 'yaml');
    } else if (this.format === 'xml') {
      cb(null, js2xmlparser('user', json), 'xml');
    } else if (this.format === 'prettyjson' || this.format === 'pretty') {
      cb(null, JSON.stringify(json, null, 2), 'json');
    } else if (this.format === 'csv') {
      converter.json2csv(json.results, (err, csv) => {
        cb(null, csv, 'csv');
      });
    } else if (this.format === 'raw') {
      cb(null, json.results, 'txt');
    } else if (this.format === 'prettyraw') {
      cb(null, JSON.stringify(json.results, null, 2), 'json');
    } else {
      cb(null, JSON.stringify(json), 'json');
    }
  } else {
    // Errors caused by code wrapped around broken api code
    // which is basically unexpected end of input and more clear
    // to the user
    if ([
      "SyntaxError: Unexpected token }",
      "SyntaxError: Unexpected token catch",
github lob / lob-node / examples / verify_and_create_letters_from_csv / index.js View on Github external
.then((letter) => {
      console.log(`Successfully sent a letter to ${   client.name}`);
      client.letter_id = letter.id;
      client.letter_url = letter.url;
      converter.json2csv(client, (err, csv) => {
        if (err) {
          throw err;
        }
        fs.write(successFd, csv);
      }, { PREPEND_HEADER: false });
    })
    .catch(() => {
github RandomAPI / Randomuser.me-Node / api / 1.0 / api.js View on Github external
results: this.results,
          page: this.page,
          version: this.version
        }
      };
    
      if (this.noInfo) delete json.info;
    
      if (this.format === 'yaml') {
        resolve({output: YAML.stringify(json, 4), ext: "yaml"});
      } else if (this.format === 'xml') {
        resolve({output: js2xmlparser('user', json), ext: "xml"});
      } else if (this.format === 'prettyjson' || this.format === 'pretty') {
        resolve({output: JSON.stringify(json, null, 2), ext: "json"});
      } else if (this.format === 'csv') {
        converter.json2csv(json.results, (err, csv) => {
          resolve({output: csv, ext: "csv"});
        });
      } else {
        resolve({output: JSON.stringify(json), ext: "json"});
      }
    });
  }
github datosgobar / consulta-publica / ext / lib / comments / index.js View on Github external
`"${escapeTxt(comment.text)}"`,
                  `"${escapeTxt(comment.author.fullName)}"`,
                  `"${usersRaw.find((u) => u.id === comment.author.id).email}"`,
                  reply.id,
                  `"${(reply.createdAt && escapeTxt(moment(reply.createdAt, '', req.locale).format('LL')))}"`,
                  `"${(reply.createdAt && escapeTxt(moment(reply.createdAt, '', req.locale).format('LT')))}"`,
                  reply.createdAt,
                  `"${escapeTxt(reply.text)}"`,
                  `"${escapeTxt(reply.author.fullName)}"`,
                  `"${!reply.author.id ? '' : usersRaw.find((u) => u.id === reply.author.id).email}"`
                ])
              })
            })
          })

          json2csv(commentsData, function (err, csv) {
            if (err) throw new Error('comments.csv: array to csv error')
            res.status(200)
            res.set({
              'Content-Type': 'text/csv; charset=utf-8',
              'Content-Disposition': 'attachment; filename=' + req.forum.name.replace(/\s/g, '-') + '.csv'
            })
            res.write(csv)
            res.end()
          }, { prependHeader: false })
        })
        .catch(next)
github datosgobar / consulta-publica / lib / api-v2 / comments / csv.js View on Github external
`"${escapeTxt(moment(comment.createdAt, '', req.locale).format('LT'))}"`,
                comment.createdAt,
                `"${escapeTxt(comment.text)}"`,
                `"${escapeTxt(comment.author.fullName)}"`,
                reply.id,
                `"${(reply.createdAt && escapeTxt(moment(reply.createdAt, '', req.locale).format('LL')))}"`,
                `"${(reply.createdAt && escapeTxt(moment(reply.createdAt, '', req.locale).format('LT')))}"`,
                reply.createdAt,
                `"${escapeTxt(reply.text)}"`,
                `"${escapeTxt(reply.author.fullName)}"`
              ])
            })
        })
      })

      json2csv(commentsData, function (err, csv) {
        if (err) throw new Error('comments.csv: array to csv error')
        res.status(200)
        res.set({
          'Content-Type': 'text/csv; charset=utf-8',
          'Content-Disposition': 'attachment; filename=' + req.forum.name.replace(/\s/g, '-') + '.csv'
        })
        res.write(csv)
        res.end()
      }, { prependHeader: false })
    }).catch(next)
})
github NodeBB / NodeBB / src / controllers / user.js View on Github external
const json2csv = util.promisify(function (payload, options, callback) {
	converter.json2csv(payload, callback, options);
});
github geosolutions-it / MapStore2 / web / client / epics / widgets.js View on Github external
.do( ({data = [], title = "data"}) =>
                converter.json2csv(data, (err, csv) => err ? null : saveAs(new Blob([
                    csv
                ], {type: "text/csv"}), title + ".csv")))
            .filter( () => false),
github RandomAPI / Randomuser.me-Node / api / 1.3 / api.js View on Github external
results: this.results,
          page: this.page,
          version: this.version
        }
      };
    
      if (this.noInfo) delete json.info;
    
      if (this.format === 'yaml') {
        resolve({output: YAML.stringify(json, 4), ext: "yaml"});
      } else if (this.format === 'xml') {
        resolve({output: js2xmlparser('user', json), ext: "xml"});
      } else if (this.format === 'prettyjson' || this.format === 'pretty') {
        resolve({output: JSON.stringify(json, null, 2), ext: "json"});
      } else if (this.format === 'csv') {
        converter.json2csv(json.results, (err, csv) => {
          resolve({output: csv, ext: "csv"});
        });
      } else {
        resolve({output: JSON.stringify(json), ext: "json"});
      }
    });
  }

json-2-csv

A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.

MIT
Latest version published 1 month ago

Package Health Score

80 / 100
Full package analysis

Popular json-2-csv functions