Search code examples
jsonpathaws-step-functionsjson-path-expression

Use JSONPath to extract nodes from different levels


Given this document:

{
  "k1": "v1",
  "k2": "v2",
  "k3": "v3",
  "k4": {
    "k4.1": "v4.1",
    "k4.2": "v4.2",
    "k4.3": "v4.3"
  }
}

I need to extract this subset, in this exact form:

{
  "k2": "v2",
  "k3": "v3",
  "k4.1": "v4.1"
  "k4.2": "v4.2"
}

But despite searching/experimenting for over a day, I am unable to discern the proper incantation.

This expression: $['k2', 'k3'] gives me the two top level elements:

{
  "k2" : "v2",
  "k3" : "v3"
}

And this expression: $['k4']['k4.1','k4.2'] gives me the two nested elements:

{
  "k4.1" : "v4.1",
  "k4.2" : "v4.2"
}

But how to combine these two expressions into one that includes the results from multiple levels?

Unfortunately the nature of the tool I am using (AWS State Language paths) demands that this be done with a single xpath expression.


Solution

  • I think the reason it is not working is the Path only specifies a single path, you can't append multiple paths in a single expression.

    You could use Parameters to define a custom/static schema for your input to a task. The flow is as follows State-Input->InputPath->Parameters.

    Using the input you supplied, and a Lambda Function which just echoes the input back, the following worked for me:

    {
        "StartAt": "HelloWorld",
        "States": {
            "HelloWorld": {
                "Type": "Task",
                "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXX:function:LambdaFunctionToEchoInput",
                "Parameters": {
                    "k2.$": "$.k2",
                    "k3.$": "$.k3",
                    "k4.1.$": "$.k4.['k4.1']",
                    "k4.2.$": "$.k4.['k4.2']"
                },
                "End": true
            }
        }
    }