Search code examples
jsonata

How do I Replace array of conditional values using JSONata


I have a large array of "Application Status" values that need to be replaced based upon conditional statements.

F.ex.

  1. If the app status is "Approved" or "Missing in DocuPhase" or "On hold - Pending Funding" or "Only Missing Owner Clearance" or "Pending Payment Processing" or "Pending Quality Control Review" or "Reviewed", the Portal status = Under Review
  2. If app status is "On Hold - Funding Exhausted", the Portal status = Exhausted
  3. If the app status is "Denial Pending" or "Denial PQCR" or "Review Finalized", the Portal status = Denied
  4. If the app status is "Payment Import" or "Payment Sent", the Portal status = Disbursed
  5. Else (any other app status) = Requested

I was trying something like this:

$replace(result.fields[label = "R.Application Status"].values.value.text, ["Approved", "Missing in DocuPhase", "On hold - Pending Funding", "Only Missing Owner Clearance", "Pending Payment Processing", "Pending Quality Control Review", "Reviewed"], "Under Review")

However, this does not work and I want to get away from doing something ugly like this:

$replace($replace($replace($replace($replace($replace($replace($replace(result.fields[label = "R.Application Status"].values.value.text, "Approved", "Under Review"), "Missing in DocuPhase", "Under Review"), "On hold - Pending Funding", "Under Review"), "Only Missing Owner Clearance", "Under Review"), "Pending Payment Processing", "Under Review"), "Pending Quality Control Review", "Under Review"), "Reviewed", "Under Review")

Solution

  • If you only need to get a list of statuses parsed from the original object structure with certain statuses replaced, you can do it with a regular expression passed down to the $replace function like this: https://stedi.link/RMFB0yj

    Alternatively, you can achieve the same if you use a ternary expression within a map operator: https://stedi.link/bg96HrE

    If you need to preserve the original structure and only want certain statuses to be replaced, then I would do it like that: https://stedi.link/wB5f9tu