npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ddbjson

v1.2.0

Published

Marshall/unmarshall, convert JSON from/to DynamoDB JSON on terminal

Downloads

670

Readme

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

| type | foodItems | | ----- | ------------------------------ | | 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`