jsonjsonpathaws-step-functions

How to filter JSON objects using JSONPath when some objects may lack certain fields?


I'm currently working on filtering JSON objects using JSONPath, but I'm encountering an issue when some objects in the JSON structure may lack certain fields altogether. Specifically, I'm trying to filter a JSON structure containing information about animal calves, where in some cases all calves may have for example only "Alive" HealthStatus.

Here's an example of the JSON structure:

{
  "EventType": "Calving",
  "CalvingDate": "2019-01-01",
  "AnimalOfficialRegNumber": "FI1231928414",
  "ObjectGuid": "dfjadasd-12312afd-123-asd-sda",
  "CalvingDifficulty": "Easy",
  "LactationNumber": "1",
  "Calves": [
    {
      "Guid": "123456789",
      "HealthStatus": "Alive"
    },
    {
      "Guid": "987654321",
      "HealthStatus": "Stillborn"
    }
  ]
}

I want to filter the "Calves" array based on the "HealthStatus" field. Here's the JSONPath query I'm currently using:

{
  "alive.$": "$.Calves[?(@.HealthStatus == Alive)]]",
  "stillBorn.$" : "$.Calves[?(@.HealthStatus == Stillborn)]]",
  "notExisting.$": "$.Calves[?(@.HealthStatus == NotExisting)]]"
}

However, when I run this query, I encounter issues with the "notExisting.$" part:

No results for path: '$.Calves[?(@.HealthStatus == NotExisting)]]' specified for the field 'notExisting.$'  in input JSON.

How can I modify my JSONPath query to properly filter the "Calves" array, considering that some calves may not have a "HealthStatus" set to one of the values in the JSONPath query? Is there a way to achieve this without using negative conditions?

Any insights or alternative approaches would be greatly appreciated. Thanks in advance!

I've read through the AWS blog articles (this and this) and official documentation but they lack information on such cases. There is also little to none info on the internet or they explore similar cases to those from the articles.


Solution

  • My colleague found that the error occurs only in the Data Flow Simulator provided by AWS and not in the concrete implementation, so one can use this filter without the fear of constantly getting an error instead of an empty array.