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?

0

1 Answer 1

3

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)]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.