0

I am using AWS Step Functions which utilizes JSONPath for providing JSON paths. I have the following input :

{
    "response": {
      "isSuccess": true,
      "error": "",
      "body": {
        "count": 2,
        "fields": [
          {
            "fieldId": 1,
            "tabId": 100,
            "title": "First Name"
          },
          {
            "fieldId": 2,
            "tabId": 100,
            "title": "Last Name"
          }
        ]
      }
    },
    "iteration": {
      "totalCount": 2,
      "currentCount": 0,
      "step": 1
    }
  }

I want to query the fields array as:

$.response.body.fields[$.iteration.currentCount]

The value of currentCount is incremented by 1 as part of an iteration. I am getting an invalid XPath exception when trying to use the above.

Can someone please advice on how to provide a dynamic property value to read array values?

Furqan Shaikh
  • 371
  • 2
  • 4
  • 13

1 Answers1

1

As described on https://github.com/json-path/JsonPath#operators you can index an array with a number only. However, you can use a filter expression to select a specific item from the array as follows.

Assuming you have another field that denotes the index such as:

{
  "index": 0,
  "fieldId": 2,
  "tabId": 100,
  "title": "Last Name"
}

You can then do

$.response.body.fields[?(@.index==$.iteration.currentCount)]

Selçuk Cihan
  • 1,979
  • 2
  • 17
  • 30