2

I am trying to pass pass parameter with space to aws cloudformation create-stack aws cli.

The issue is that my parameter has space. I am using powershell for scripting.

Below is example of my parameter

 $JsonParameter = '[{"ParameterKey":"name","ParameterValue":"John"},{"ParameterKey":"Occupation","ParameterValue":"Test Engineer"}]'| ConvertTo-Json

This returns

"[{\"ParameterKey\":\"name\",\"ParameterValue\":\"John\"},{\"ParameterKey\":\"Occupation\",\"ParameterValue\":\"Test Engineer\"}]"

cli command is

aws cloudformation create-stack --stack-name $stackName --template-url $templateUrl --capabilities $capabilityList --parameters $JsonParameter --region "us-east-1"

The error goes

Error parsing parameter '--parameters': Invalid JSON:
[{"ParameterKey":"name","ParameterValue":"John"},{"ParameterKey":"Occupation","ParameterValue":"Test

From the error, it looks like cli doesn't like the space in the ParameterValue. How do I escape the space, so that cli doesn't complain about the space in the value?

3
  • Show the full command line that you passed to create-stack. Commented Sep 23, 2019 at 2:43
  • I have the same issue, I'm trying to pass an ssh public key... Commented Feb 7, 2020 at 10:23
  • 1
    See my answer. You immediate problem is the use of | ConvertTo-Json. This is solely to convert a Powershell object to Json. Your string is already a json string. You can pass it as is. I included an example where you'd want to use | ConvertTo-Json Commented Feb 7, 2020 at 11:26

1 Answer 1

3
+300

Remove | ConvertTo-Json.

Your string is already a json string so you do not want to perform a conversion.

$JsonParameter = '[{"ParameterKey":"name","ParameterValue":"John"},{"ParameterKey":"Occupation","ParameterValue":"Test Engineer"}]'

Just use the string as is.

Alternate scenario

Should you be working with a Powershell object rather than a json string, you might want at some point to convert it into a json to pass it as parameter to your aws call.

That's the moment where ConvertTo-Json would reveal itself to be useful.

Take this for instance

$JsonParameter = @(
    @{
        ParameterKey   = 'name'
        ParameterValue = 'John'
    },
    @{
        ParameterKey = 'Occupation'
        ParameterValue = 'Test Engineer'
    }
) 

This is a Powershell object, for which you might had, in a different context, built from scratch with the intent of passing it as a json paramerter to your aws call.

Now, to achieve the transition from this state of "array of hashtables" to a valid json string, you need to use the ConvertTo-Json cmdlet.

$JsonParameterString = $JsonParameter | Convertto-json -Compress

The resulting string he same as you had initially, ready to be passed down to aws :

[{"ParameterKey":"name","ParameterValue":"John"},{"ParameterKey":"Occupation","ParameterValue":"Test Engineer"}]

If on the other hand, you had a json string and needed to edit it without fuss, you could use the ConvertFrom-Json cmdlet, then edit the resulting object as needed and convert it back to json again before passing it down.

Additional note

In my Powershell to Json example, I used the -compress switch parameter. This is optional. This will create a compressed json string (one line) instead of an expanded one.

Reference

Powershell doc - ConvertTo-Json

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for clarification. -Compress worked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.