JSON to CSV Converter avatar
JSON to CSV Converter
Deprecated
View all Actors
This Actor is deprecated

This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?

See alternative Actors
JSON to CSV Converter

JSON to CSV Converter

vaclavrut/bigjson2csv

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.

Code

1const Apify = require('apify');
2const rp = require('request-promise')
3const Promise = require('bluebird')
4const ApifyClient = require('apify-client');
5
6Apify.main(async () => {
7
8    const input = await Apify.getValue('INPUT');
9    const environmentVariables = await Apify.getEnv()
10
11    const apifyClient = new ApifyClient({
12        userId: environmentVariables.userId,
13        token: environmentVariables.token
14});
15    const datasets = apifyClient.datasets;
16
17    await Promise.map(input.urls, async (solve) => {
18
19        if (!solve.name || !solve.url) throw new Error('Invalid input! Please provide combination of name and url');
20
21        const inputData = await rp({uri: solve.url});
22
23        const dataset = await datasets.getOrCreateDataset({
24            datasetName: solve.name + environmentVariables.actRunId,
25        });
26
27        const parsedData = JSON.parse(inputData);
28        console.log("Loaded", parsedData.length, " for ", solve.name);
29
30        while (parsedData.length) {
31            console.log("Remaining records for", solve.name, " is: ", parsedData.length)
32            await datasets.putItems({datasetId: dataset.id,data: parsedData.splice(0, 1000)});
33        }
34        console.log(solve.name," finished.")
35        console.log("Download a CSV : https://api.apify.com/v2/datasets/" + dataset.id + "/items?format=csv&attachment=1");
36        console.log("Download a XLSX : https://api.apify.com/v2/datasets/" + dataset.id + "/items?format=xlsx&attachment=1");
37
38    }, {concurrency: 10})
39
40    console.log("Job finished, see you next time.");
41});

Actor expects the file on the input in this structure:

1[{
2	"name":"Here is a name of the object 1.",
3	"value1": 1,
4	"value2":2,
5	"end":"Thanks for watching!"
6},
7{
8	"name":"Here is a name of the object 2.",
9	"value1": 1,
10	"value2":2,
11	"end":"Thanks for watching!"
12}]
Developer
Maintained by Community
Categories