Skip to content

Instantly share code, notes, and snippets.

@M1ke
Created February 12, 2024 20:35
Show Gist options
  • Save M1ke/2188e0fb4d53225a62dc36e455611d47 to your computer and use it in GitHub Desktop.
Save M1ke/2188e0fb4d53225a62dc36e455611d47 to your computer and use it in GitHub Desktop.
~/.jq for handy jq pipes
# https://stackoverflow.com/a/28641626/518703
def decode_ddb:
def _sprop($key): select(keys == [$key])[$key]; # single property objects only
((objects | { value: _sprop("S") }) # string (from string)
// (objects | { value: _sprop("NULL") | null }) # null (from boolean)
// (objects | { value: _sprop("B") }) # blob (from string)
// (objects | { value: _sprop("N") | tonumber }) # number (from string)
// (objects | { value: _sprop("BOOL") }) # boolean (from boolean)
// (objects | { value: _sprop("M") | map_values(decode_ddb) }) # map (from object)
// (objects | { value: _sprop("L") | map(decode_ddb) }) # list (from encoded array)
// (objects | { value: _sprop("SS") }) # string set (from string array)
// (objects | { value: _sprop("NS") | map(tonumber) }) # number set (from string array)
// (objects | { value: _sprop("BS") }) # blob set (from string array)
// (objects | { value: map_values(decode_ddb) }) # all other non-conforming objects
// (arrays | { value: map(decode_ddb) }) # all other non-conforming arrays
// { value: . }).value # everything else
;
# https://stackoverflow.com/a/69754759/518703 (also proceed with "| column -ts $'\t'")
def pretty_table:
(.[0]|keys_unsorted|(.,map(length*"-"))),.[]|map(.)|@tsv
;
@M1ke
Copy link
Author

M1ke commented Feb 12, 2024

Two tools (so far) for doing nice DDB output on CLI. E.g.

aws dynamodb query --table-name <table> \
  --key-condition-expression "<hash key> = :val" \
  --expression-attribute-values '{":val":{"S":"<val>"}}' \
| jq -r '.Items | decode_ddb | pretty_table' \
| column -ts $'\t'

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