Skip to content

Instantly share code, notes, and snippets.

@mtliendo
Created April 17, 2019 13:37
Show Gist options
  • Save mtliendo/0d7f40fe3bcab3311b985853a6539450 to your computer and use it in GitHub Desktop.
Save mtliendo/0d7f40fe3bcab3311b985853a6539450 to your computer and use it in GitHub Desktop.
Export and Convert Dynamo Table to JavaScript JSON
//! How to get all records from a Dynamo DB Table and store as regular JSON
// 1. Run the following command in the terminal
// * Note that the output will be in Dynamo JSON format
// aws dynamodb scan --region REGION --profile PROFILE_NAME --table-name TABLE_NAME > exports.json
// 2. Convert from Dynamo JSON to regular JSON.
const AWS = require('aws-sdk')
const fs = require('fs')
const tableData = JSON.parse(fs.readFileSync('./exports.json', 'utf-8'))
const numItems = tableData.Count
const initialWrite = '['
fs.writeFileSync('exportedData.json', initialWrite)
tableData.Items.forEach((item, index) => {
const atEnd = numItems - 1 === index
const endValue = atEnd ? ']' : ','
fs.appendFileSync(
'exportedData.json',
JSON.stringify(AWS.DynamoDB.Converter.unmarshall(item), null, 2) + endValue
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment