Created
March 18, 2021 04:31
-
-
Save jamessouth/7bd05a4f21c5eeda21754be05f645809 to your computer and use it in GitHub Desktop.
Node script to display JSON from the AWS CLI (DynamoDB) in a more readable table
This file contains 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
const readable = process.stdin; | |
const chunks = []; | |
readable.on('readable', () => { | |
let chunk; | |
while (null !== (chunk = readable.read())) { | |
chunks.push(chunk); | |
} | |
}); | |
readable.on('end', () => { | |
const content = chunks.join(''); | |
const { Items } = JSON.parse(content); | |
const data = Items.map(i => { | |
const obj = {}; | |
for (const p in i) { | |
const val = Object.values(i[p])[0]; | |
const half = val.length > 19 ? val.length / 2 : 0; | |
obj[p] = val.slice(half); | |
} | |
return obj; | |
}); | |
console.table(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're developing with DynamoDB and you're checking the state of your data with
aws dynamodb scan --table-name tableName
, the AWS CLI, by default, returns pretty-printed JSON......which takes up a lot of space, requiring you to paginate and scroll. You can opt to return the data in a table...
...but this still doesn't look very good, like data you might see in an article. This script will take your JSON piped in from the CLI and put it in a table you can actually read:
Beautiful. The script strips out the types and shortens long values for a better fit when you've got your editor on half of your screen. To use:
aws dynamodb scan --table-name tableName | node path/to/this/file.js
.