-
-
Save weisisheng/af7bf381de828d8d8499abf2fb35890f to your computer and use it in GitHub Desktop.
Export and Convert Dynamo Table to JavaScript JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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