# CfnJson

Construct · Stable

Captures a synthesis-time JSON object a CloudFormation reference which resolves during deployment to the resolved values of the JSON object.

The main use case for this is to overcome a limitation in CloudFormation that
does not allow using intrinsic functions as dictionary keys (because
dictionary keys in JSON must be strings). Specifically this is common in IAM
conditions such as `StringEquals: { lhs: "rhs" }` where you want "lhs" to be
a reference.

This object is resolvable, so it can be used as a value.

This construct is backed by a custom resource.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › CfnJson
Implements: [IResolvable](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.IResolvable.html), [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)

- Source: [`core/lib/cfn-json.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/core/lib/cfn-json.ts#L31)
- Type last changed: v2.79.0 · 10 May 2023

## Example

TypeScript:

```ts
const tagParam = new CfnParameter(this, 'TagName');

const stringEquals = new CfnJson(this, 'ConditionJson', {
  value: {
    [`aws:PrincipalTag/${tagParam.valueAsString}`]: true,
  },
});

const principal = new iam.AccountRootPrincipal().withConditions({
  StringEquals: stringEquals,
});

new iam.Role(this, 'MyRole', { assumedBy: principal });
```

Python:

```python
tag_param = CfnParameter(self, "TagName")

string_equals = CfnJson(self, "ConditionJson",
    value={
        "f"aws:PrincipalTag/{tagParam.valueAsString}"": True
    }
)

principal = iam.AccountRootPrincipal().with_conditions({
    "StringEquals": string_equals
})

iam.Role(self, "MyRole", assumed_by=principal)
```

Java:

```java
CfnParameter tagParam = new CfnParameter(this, "TagName");

CfnJson stringEquals = CfnJson.Builder.create(this, "ConditionJson")
        .value(Map.of(
                String.format("aws:PrincipalTag/%s", tagParam.getValueAsString()), true))
        .build();

PrincipalBase principal = new AccountRootPrincipal().withConditions(Map.of(
        "StringEquals", stringEquals));

Role.Builder.create(this, "MyRole").assumedBy(principal).build();
```

C#:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var tagParam = new CfnParameter(this, "TagName");

var stringEquals = new CfnJson(this, "ConditionJson", new CfnJsonProps {
    Value = new Dictionary<string, boolean> {
        { $"aws:PrincipalTag/{tagParam.valueAsString}", true }
    }
});

var principal = new AccountRootPrincipal().WithConditions(new Dictionary<string, object> {
    { "StringEquals", stringEquals }
});

new Role(this, "MyRole", new RoleProps { AssumedBy = principal });
```

Go:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
tagParam := awscdk.NewCfnParameter(this, jsii.String("TagName"))

stringEquals := awscdk.NewCfnJson(this, jsii.String("ConditionJson"), &CfnJsonProps{
	Value: map[string]*bool{
		fmt.Sprintf("aws:PrincipalTag/%v", tagParam.valueAsString): jsii.Boolean(true),
	},
})

principal := iam.NewAccountRootPrincipal().WithConditions(map[string]interface{}{
	"StringEquals": stringEquals,
})

iam.NewRole(this, jsii.String("MyRole"), &RoleProps{
	AssumedBy: principal,
})
```

## Initializer

TypeScript:

```ts
new CfnJson(scope: Construct, id: string, props: CfnJsonProps)
```

Python:

```python
CfnJson(scope: Construct, id: str, props: CfnJsonProps)
```

Java:

```java
new CfnJson(Construct scope, String id, CfnJsonProps props)
```

C#:

```csharp
new CfnJson(Construct scope, string id, CfnJsonProps props)
```

Go:

```go
NewCfnJson(Scope Construct, Id *string, Props CfnJsonProps)
```

## Construct Props

- `value`: any (required) — The value to resolve.

  Can be any JavaScript object, including tokens and
  references in keys or values.

## Properties

- `creationStack`: string[] — The creation stack of this resolvable which will be appended to errors thrown during resolution.

  This may return an array with a single informational element indicating how
  to get this property populated, if it was skipped for performance reasons.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `value`: [Reference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Reference.html) — An Fn::GetAtt to the JSON object passed through `value` and resolved during synthesis.

  Normally there is no need to use this property since `CfnJson` is an
  IResolvable, so it can be simply used as a value.

## Methods

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `resolve(_context: IResolveContext): any` — Produce the Token's value at resolution time.

- `toJSON(): string` — This is required in case someone JSON.stringifys an object which references this object. Otherwise, we'll get a cyclic JSON reference.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
