0

I am struggling to get formatted output from an aws dynamodb scan command. An item in the dynamodb table looks like below:

{
   "labels": {
    "Category": [
      "Data",
      "EMR"
    ],
   "Environment": "NonProd",
   "Severity": "Critical"
   },
  "subscriber_id": "blah@blah.com",
  "subscriber_type": "email"
}

When I run the query:

aws dynamodb scan --table-name dummy_table --region region_name -- 
profile default --query "Items[?subscriber_id.S == 
'blah@blah.com'].labels.M[]"

I get the output as below:

[
{
    "Environment": {
        "S": "NonProd"
    },
    "Severity": {
        "S": "Critical"
    },
       "Category": {
            "L": [
                {
                   "S": "Data"
                },
               {
                    "S": "EMR"
               }
            ]
       }
    }
]

Desired output is:

    {
        "Category": [
        "Data",
        "EMR"
        ],
       "Environment": "NonProd",
       "Severity": "Critical"
    }

To achieve the desired output, I tried to manipulate using jq.

Updated query:

    aws dynamodb scan --table-name dummy_table --region 
 region_name -- 
   profile default --query "Items[?subscriber_id.S == 
   'blah@blah.com'].labels.M[]" |
   jq -r '.[] 
     | to_entries[] 
     | [{key:.key, value:.value[]}] 
     | from_entries' | jq -s add

Output is:

{
  "Environment": "NonProd",
  "Severity": "Critical",
  "Category": [
    {
      "S": "Data"
    },
    {
      "S": "EMR"
    }
  ]
}

As you see it's close, but it's not processing the Category list. Any help with getting the desired output is appreciated.

Thanks

peak
  • 105,803
  • 17
  • 152
  • 177
user1470220
  • 123
  • 2
  • 15
  • Possible duplicate of [How to simplify aws DynamoDB query JSON output from the command line?](https://stackoverflow.com/questions/28593471/how-to-simplify-aws-dynamodb-query-json-output-from-the-command-line) – Jeff Mercado Jun 06 '19 at 15:53

1 Answers1

1

You can use the following :

.[] | { Environment: .Environment.S, Severity: .Severity.S, Category: (.Category.L | map(.S)) }

Output :

{
  "Environment": "NonProd",
  "Severity": "Critical",
  "Category": [
    "Data",
    "EMR"
  ]
}

Try it here !

Aaron
  • 24,009
  • 2
  • 33
  • 57