Skip to content

Instantly share code, notes, and snippets.

@titanjer
Created December 22, 2023 08:26
Show Gist options
  • Save titanjer/1baa544c0dd57b5e0bfb1eeac171c190 to your computer and use it in GitHub Desktop.
Save titanjer/1baa544c0dd57b5e0bfb1eeac171c190 to your computer and use it in GitHub Desktop.
Dynamodb Marshall by JQ
def marshall:
def m:
def to_string: {S:.};
def to_number: {N:.|tostring};
def to_boolean: {BOOL:.};
def to_array: {L:.|to_entries|map(.value|m)};
def to_object: {M:.|map_values(m)};
def to_null: {NULL:true};
if .|type=="string" then .|to_string
elif .|type=="number" then .|to_number
elif .|type=="boolean" then .|to_boolean
elif .|type=="array" then .|to_array
elif .|type=="object" then .|to_object
elif .|type=="null" then .|to_null
else . end;
.|map_values(m);
@titanjer
Copy link
Author

➜ jq 'include "modules"; . | marshall' <<< '{"name":"Jane","age":42, "colors": ["red", "green"],"items":{"a": 1}}'
{
  "name": {
    "S": "Jane"
  },
  "age": {
    "N": "42"
  },
  "colors": {
    "L": [
      {
        "S": "red"
      },
      {
        "S": "green"
      }
    ]
  },
  "items": {
    "M": {
      "a": {
        "N": "1"
      }
    }
  }
}

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