Fn::ToJsonString - AWS CloudFormation (original) (raw)

The Fn::ToJsonString intrinsic function converts an object or array to its corresponding JSON string.

Declaration

JSON

{ "Fn::ToJsonString": Object }
{ "Fn::ToJsonString": Array }

YAML

Fn::ToJsonString: Object
Fn::ToJsonString: Array

Parameters

Object

The object you want to convert to a JSON string.

Array

The array you want to convert to a JSON string.

Return value

The object or array converted to a JSON string.

Examples

Convert an object to a JSON string

This example snippet converts the object passed to the intrinsic function to a JSON string.

JSON

{
//...
    "Transform": "AWS::LanguageExtensions"
    //...
        "Fn::ToJsonString": {
            "key1": "value1",
            "key2": { 
                "Ref": "ParameterName"
            }
        }
//...
}

YAML

Transform: 'AWS::LanguageExtensions'
#...
  Fn::ToJsonString: 
    key1: value1
    key2: !Ref ParameterName
#...

In both of these examples, if the Ref toParameterName resolves to resolvedValue, the function resolves to the following JSON string:

"{\"key1\":\"value1\",\"key2\":\"resolvedValue\"}"

Convert an array to a JSON string

This example snippet converts the array passed to the intrinsic function to a JSON string.

JSON

{
//...
    "Transform": "AWS::LanguageExtensions"
    //...
        "Fn::ToJsonString": [{
            "key1": "value1",
            "key2": { 
                "Ref": "ParameterName" 
            }
        }]
//...
}

YAML

Transform: 'AWS::LanguageExtensions'
#...
  Fn::ToJsonString: 
    - key1: value1
      key2: !Ref ParameterName
#...

In both of these examples, if the Ref toParameterName resolves to resolvedValue, the function resolves to the following JSON String:

"[{\"key1\":\"value1\"},{\"key2\":\"resolvedValue\"}]"

Supported functions

You can use the following functions in the Fn::ToJsonString intrinsic function or array: