1.2.0 ā€¢ Published 12 months ago

ddbjson v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

šŸ’« JSON from/to DynamoDB JSON on CLI

npm node tests coverage status license

Use ddbjson to convert from DynamoDB JSON format to regular JSON and vice versa (marshall/unmarshall) on any terminal:

  • šŸ”„ Works on all platforms!
  • šŸ“„ Convert a file from given file path!
  • āœļø Convert a JSON string!
  • ā›“ Read from stdin: pipe in JSON from another command!
  • šŸ° Read and convert only a subset of the JSON!
  • šŸ¤ The output can be piped or redirected to other commands!
  • šŸ§° Integrate it into your workflow, when using AWS DynamoDB CLI!

šŸ’” See usage below! šŸ‘‡

šŸ”© Installation

yarn global add ddbjson
npm i -g ddbjson

šŸŽ³ Requirements

  • NodeJS 12+

šŸ”‘ Usage

ddbjson <command> [options] <json>

Commands:
  ddbjson unmarshall, u    Converts a DynamoDB JSON format to regular JSON
  ddbjson marshall, m      Converts a regular JSON to a DynamoDB JSON format

Options:
  -g <path>                Parse only a subset of given JSON by passing the path to a property

Usage examples

šŸ”¶ Unmarshall: convert from DynamoDB JSON to regular JSON

āŒØļø Command: unmarshall or u

šŸ”ø Unmarshall from a JSON file

# food.json
{
  "fruits": {
    "L": [
      { "S": "apple" },
      { "S": "kiwi" }
    ]
  }
}
# read from the file:
$ ddbjson u food.json

# {"fruits":["apple","kiwi"]}

šŸ”ø Unmarshall from a JSON string

$ ddbjson u '{"fruits":{"L":[{"S":"apple"},{"S":"kiwi"}]}}'

# {"fruits":["apple","kiwi"]}

šŸ”ø Unmarshall from stdin

$ aws dynamodb get-item --table-name food --key '{"type":{"S":"fruit"}}' | ddbjson u -g "Item" -

# {"fruits":["apple","kiwi"]}

šŸ”· Marshall: convert from regular JSON to DynamoDB JSON

āŒØļø Command: marshall or m

šŸ”¹ Marshall from a JSON file

# food.json
{
  "fruits": [
    "apple",
    "kiwi"
  ]
}
# read from the file:
$ ddbjson m food.json

# {"fruits":{"L":[{"S":"apple"},{"S":"kiwi"}]}}

šŸ”¹ Marshall from a JSON string

$ ddbjson m '{"fruits":["apple","kiwi"]}'

# {"fruits":{"L":[{"S":"apple"},{"S":"kiwi"}]}}

šŸ”¹ Marshall from stdin

$ curl https://food.com/api | ddbjson m -

# {"fruits":{"L":[{"S":"apple"},{"S":"kiwi"}]}}

šŸ° Parse only a subset of JSON using -g

  • Use dot notation to pass in a property path.
  • Useful when reading from AWS CLI DynamoDB commands (read more).
  • Get properties of objects, array item by index or * for all items.
# food.json
{
  "fruits": [
    {
      "name": "apple",
      "color": "red",
      "benefits": ["fiber","vitamin b","vitamin e"]
    },
    {
      "name": "kiwi",
      "color": "green",
      "benefits": ["potassium","vitamin e","vitamin c"]
    }
  ]
}

Convert only an item in the array

$ ddbjson m -g "fruits.0.benefits" food.json

# [{"S":"fiber"},{"S":"vitamin b"},{"S":"vitamin e"}]

Convert all items in the array

$ ddbjson m -g "fruits.*.name" food.json

# [{"S":"apple"},{"S":"kiwi"}]

Note: -g "fruits.*" and -g "fruits" are the same because they both return all items in the array.

āš ļø When reading from AWS CLI DynamoDB commands

Given this food table:

typefoodItems
fruit[{"S":"apple"},{"S":"kiwi"}]

āš”ļø aws dynamodb get-item

AWS CLI wraps the table output in the Item property:

$ aws dynamodb get-item --table-name food --key '{"type":{"S":"fruit"}}' 

# {
#   "Item": {
#     "foodItems": {
#       "L": [
#         {
#           "S": "apple"
#         },
#         ...

Use -g "Item" when unmarshalling output from aws dynamodb get-item:

$ aws dynamodb get-item --table-name food --key '{"type":{"S":"fruit"}}'  | ddbjson u -g "Item" -

# {"foodItems":["apple","kiwi"],"type":"fruit"}

āš”ļø aws dynamodb scan

AWS CLI wraps the table output in the "Items" property:

$ aws dynamodb scan --table-name food

{
  "Items": [
    {
      "type": { "S": "fruit" },
      "foodItems": {
        "L": [
            { "S": "apple" },
            { "S": "kiwi" }
            ...

Use -g "Items" to unmarshall output from aws dynamodb scan.

$ aws dynamodb scan --table-name food | ddbjson u -g "Items" -

# [{"foodItems":["apple","kiwi"],"type":"fruit"},...]

šŸ’” More usage ideas

šŸŒ· Format output using jq

Use jq to format the JSON output:

$ ddbjson u marshalled-food.json | jq

# {
#   "fruits": [
#     "apple",
#     "kiwi"
#   ]
# }

Redirect output to a file:

$ ddbjson u marshalled-food.json | jq > unmarshalled-food.json
$ cat unmarshalled-food.json

# {
#   "fruits": [
#     "apple",
#     "kiwi"
#   ]
# }

When combined with AWS CLI output this is really powerful:

$ aws dynamodb get-item --table-name food --key `ddbjson m '{"type":"fruit"}'` | ddbjson u -g 'Item' - | jq > result.json
$ cat result.json

# {
#   "foodItems": [
#     "apple",
#     "kiwi"
#   ],
#   "type": "fruit"
# }

ā›“ Use as part of other commands

For example, marshall the Partition key in the AWS CLI command:

$ aws dynamodb get-item --table-name food --key `ddbjson m '{"type":"fruit"}'`

šŸŽ¬ Use heredoc

Read from stdin when combined with heredocs:

$ cat << EOF | ddbjson m -               
{               
  "fruits": [
    "apple",
    "kiwi"
  ]
}
EOF

# {"fruits":{"L":[{"S":"apple"},{"S":"kiwi"}]}}

āœļø Convert regular JSON file when writing to DynamoDB

# meat.json
{
  "type": "meat",
  "foodItems": [
    "beef",
    "pork"
  ]
}

Read from regular JSON file to write to DynamoDB:

$ aws dynamodb put-item --table-name food --item `ddbjson m meat.json`