JSON to CSV Converter

  • vaclavrut/bigjson2csv
  • Modified
  • Users 107
  • Runs 1.4k
  • Created by Author's avatarVaclav Rut

Solution to convert big JSON files to CSV/Excel in JS. Paste URL for the JSON into the input along with the name of the dataset to store the records. The actor uses the dataset to handle the conversion of the items by pushing them to the dataset, which is then downloaded.


const Apify = require('apify'); const rp = require('request-promise') const Promise = require('bluebird') const ApifyClient = require('apify-client'); Apify.main(async () => { const input = await Apify.getValue('INPUT'); const environmentVariables = await Apify.getEnv() const apifyClient = new ApifyClient({ userId: environmentVariables.userId, token: environmentVariables.token }); const datasets = apifyClient.datasets; await Promise.map(input.urls, async (solve) => { if (!solve.name || !solve.url) throw new Error('Invalid input! Please provide combination of name and url'); const inputData = await rp({uri: solve.url}); const dataset = await datasets.getOrCreateDataset({ datasetName: solve.name + environmentVariables.actRunId, }); const parsedData = JSON.parse(inputData); console.log("Loaded", parsedData.length, " for ", solve.name); while (parsedData.length) { console.log("Remaining records for", solve.name, " is: ", parsedData.length) await datasets.putItems({datasetId: dataset.id,data: parsedData.splice(0, 1000)}); } console.log(solve.name," finished.") console.log("Download a CSV : https://api.apify.com/v2/datasets/" + dataset.id + "/items?format=csv&attachment=1"); console.log("Download a XLSX : https://api.apify.com/v2/datasets/" + dataset.id + "/items?format=xlsx&attachment=1"); }, {concurrency: 10}) console.log("Job finished, see you next time."); });

Actor expects the file on the input in this structure:

[{ "name":"Here is a name of the object 1.", "value1": 1, "value2":2, "end":"Thanks for watching!" }, { "name":"Here is a name of the object 2.", "value1": 1, "value2":2, "end":"Thanks for watching!" }]