alex
alex

Reputation: 11400

JSONPath for Root element

I'm using JSONPath in AWS Step Functions (playground: Parameters tab).

In this example if we have a JSON input:

{
  "movies": [
    {
      "genre": "crime",
      "director": "Quentin Tarantino",
      "title": "Reservoir Dogs",
      "year": 1992
    },
    {
      "genre": "action",
      "director": "Brian De Palma",
      "title": "Mission: Impossible",
      "year": 1996,
      "staring": [
        "Tom Cruise"
      ]
    }
  ],
  "metadata": {
    "lastUpdated": "2020-05-27T08:00:00.000Z"
  },
  "stringJson": "{\"arr\": [1, 2, 3, 4, 5], \"bool\": true, \"null\": null, \"number\": 1}"
}

and parameters like this:

{
    "test.$": "$.metadata"
}

we would get this output:

{
  "test": {
    "lastUpdated": "2020-05-27T08:00:00.000Z"
  }
}

while I would like to get

{
  "lastUpdated": "2020-05-27T08:00:00.000Z"
}

or even better, say all but one:

{
  "movies": [
    {
      "genre": "crime",
      "director": "Quentin Tarantino",
      "title": "Reservoir Dogs",
      "year": 1992
    },
    {
      "genre": "action",
      "director": "Brian De Palma",
      "title": "Mission: Impossible",
      "year": 1996,
      "staring": [
        "Tom Cruise"
      ]
    }
  ],
  "metadata": {
    "lastUpdated": "2020-05-27T08:00:00.000Z"
  }
}

How do I put an element (or all elements) in the root?

I tried parameters like:

{
    "$": "$.metadata.lastUpdated"
}

or

{
    "$": "$.(?[@ != 'stringJson'])"
}

but none of these worked for me.

Upvotes: 0

Views: 1122

Answers (1)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23612

Set InputPath to $.metadata to get:

{
  "lastUpdated": "2020-05-27T08:00:00.000Z"
}

Reference paths (the type of path that InputPath accepts) is limited in that it can only identify a single node within the entire JSON structure.

You can't natively exclude all elements except 'x' element.

Upvotes: 1

Related Questions