KRL
KRL

Reputation: 125

Convert CSV to DynamoDB JSON format

I have a trigger on an S3 bucket that has a CSV file. I am trying to write a Node.js lambda function to load the CSV file into a DynamoDB table. My file looks like this:

Speed, San Diego, 35,0,0
Air, Houston, 32,0,0
Air, Chicago, 35,0,0

My file doesn't have a header but I need to have it with the key values like below:

Gauge: Speed, City: San Diego, Value:35, temp1: 0, temp2: 0

and so on, I am basically just trying to get this into DynamoDB JSON format, once I get it into that format, I can load it, but I haven't been able to find any example scripts online. Does anyone have an example of how to get the original data into DynamoDB JSON format?

I have seen the data pipeline posts, but I'm looking for a Node.js method to convert this data to DDB JSON.

Upvotes: 1

Views: 6917

Answers (2)

Tom Melo
Tom Melo

Reputation: 1499

You can use csvtojson to convert your csv data into json object in your lambda function.

Here's a quick and dirty example for you to get the idea:

var AWS     = require('aws-sdk'),
    Promise = require('bluebird'),
    csv     = require('csvtojson'),
    uuid    = require('uuid/v4');


AWS.config.setPromisesDependency(Promise);

var save = (items) => {
    var dynamodb = new AWS.DynamoDB.DocumentClient();
    var params = {
        RequestItems: {
            'MyDynamoDBTable': items
        }
    }

    return dynamodb.batchWrite(params).promise();
}

var data = 
"Speed, San Diego, 35,0,0 \n \
Air, Houston, 32,0,0 \n \
Air, Chicago, 35,0,0";

var items = [];

csv({noheader: true})
    .fromString(data)
    .on("csv", (row) => {
        console.log(row);
        
        var item = {
            PutRequest: {
                Item: {
                    id: uuid(),
                    gauge: row[0],
                    city : row[1],
                    value: row[3],
                    temp1: row[4],
                    temp2: row[5]
                }
            }            
        };

        items.push(item);
    })
    .on('done', () => {
        save(items)
            .then((result) => console.log(result))
            .catch((error) => console.error(error));
    });

Upvotes: 0

Kuzivakwashe
Kuzivakwashe

Reputation: 36

You can parse the CSV content into a JavaScript array using the csv-parse npm module. I'm using version 1.2.0, the sync option but it should work with later versions.

const parse = require('csv-parse/lib/sync');

function parseCsv( data, header ) {
    // remove spaces in front of data
    data = data.replace(/\s*,\s*/g, ",");
    // add new line between header & data
    data = `${header}\n${data}`;

    return parse(data, {columns: true});
}

var csv = 
`Speed, San Diego, 35,0,0
Air, Houston, 32,0,0
Air, Chicago, 35,0,0`;

// comma separate your header
var header = "Gauge,City,Value,temp1,temp2";


// #TEST
var ddbArray = parseCsv(csv, header);

console.log(JSON.stringify( ddbArray, 0, 2 ));

Upvotes: 2

Related Questions