Skip to content

Instantly share code, notes, and snippets.

@jamessouth
Created March 18, 2021 04:31
Show Gist options
  • Save jamessouth/7bd05a4f21c5eeda21754be05f645809 to your computer and use it in GitHub Desktop.
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
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);
});
@jamessouth
Copy link
Author

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...

            "pk": {
                "S": "GAME"
            },
            "sk": {
                "S": "1615661234884293023"
            }

...which takes up a lot of space, requiring you to paginate and scroll. You can opt to return the data in a table...

+---------------------+---------+-----------------+
||                     Items                     ||
|||                     pk                      |||
||+----------------+----------------------------+||
|||  S             |  GAME                      |||
||+----------------+----------------------------+||
|||                     sk                      |||
||+------+--------------------------------------+||
|||  S   |  1615661234884293023                 |||
||+------+--------------------------------------+||

...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:

┌─────────┬──────────────────────┬───────────────────────┐
│ (index) │          pk          │          sk           │
├─────────┼──────────────────────┼───────────────────────┤
│    0    │        'GAME'        │ '1615661234884293023' │

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment