Search code examples
amazon-web-servicescommand-line-interfaceaws-clijmespath

How to set a default value for null values in JMESPath query when using AWS CLI?


I'm trying to run a command with AWS CLI with query. The command could be something simple like aws s3api list-buckets --query 'sum(Versions[*].Size)'

However, occasionally, some values can return null. In the example above, size can be null when there is nothing and the command will return the following error:

In function sum(), invalid type for value: None, expected one of: ['array-number'], received: "null"

How can I give it a default value? If the actual value is null, I would like to set it to 0 so that there is some value in the result instead of an error.


Solution

  • Based on the comments.

    The solution was to use:

    aws s3api list-buckets --query 'sum(Versions[*].Size || [`0`])'