zdenko.s
zdenko.s

Reputation: 1032

CDK pass step: How to add properties at root of input object

I would like to add few properties to root of input data. Assume we have

{
  "f1": 1,
  "cx": {
          "cxf1": 113,
          "cxf2": "f23"
        },
  "cx2": {
          "cxf12": 11,
          "cxf22": "f2"
        }
}

I would like to create CDK Pass step to add simple value and complex value to the root of input and pass combined data further. I should have output as:

{
  "f1": 1,
  "cx": {
          "cxf1": 113,
          "cxf2": "f23"
        },
  "cx2": {
          "cxf12": 11,
          "cxf22": "f2"
        },
  "out1": "simple",
  "out2complex": {
          "f1A": 111,
          "f2A": "f22"
        }             
}

Whatevr combination of inputPath, outputhPath, resultpath i try it does not work. It works only when result path is specified and my result will go to path as complex element.
I assume it is by design. If I specify only result, it means it will overwrite input.
Is there a way to add simple property and complex property to the root of input object and pass it further?

Upvotes: 1

Views: 1028

Answers (1)

Balu Vyamajala
Balu Vyamajala

Reputation: 10333

We need to pass the output of the pass step with resultPath

Lets say pass step output is a string simple , it will be appended to existing input Json with key out1 with resultPath: "$.out1"

const firstStep = new stepfunctions.Pass(this, "Build Out1", {
  result: stepfunctions.Result.fromString("simple"),
  resultPath: "$.out1",
});

const secondStep = new stepfunctions.Pass(this, "Build out2complex", {
  result: stepfunctions.Result.fromObject({
    f1A: 111,
    f2A: "f22",
  }),
  resultPath: "$.out2complex",
});
const def = firstStep.next(secondStep);

new stepfunctions.StateMachine(this, "StateMachine", {
  definition: def,
});

enter image description here

Input:

{
  "f1": 1,
  "cx": {
    "cxf1": 113,
    "cxf2": "f23"
  },
  "cx2": {
    "cxf12": 11,
    "cxf22": "f2"
  }
}

Output:

{
  "f1": 1,
  "cx": {
    "cxf1": 113,
    "cxf2": "f23"
  },
  "cx2": {
    "cxf12": 11,
    "cxf22": "f2"
  },
  "out1": "simple",
  "out2complex": {
    "f1A": 111,
    "f2A": "f22"
  }
}

Upvotes: 1

Related Questions