Skip to main content
June 9, 2023
Solved

Convert List of Text String to List

  • June 9, 2023
  • 8 replies
  • 0 views

I have a local variable which is receiving BacthPayloadInput Response from process model

local!updatedPurchasers_at: index(
ri!EF_GeneralUpdatePayload_cdt.RequestJson,
wherecontains(
tostring("PowerPurchasers"),
cast(103, index(ri!EF_GeneralUpdatePayload_cdt,"RequestEndpoint",{}))
),
null
),

when i output the values it contains in expression rule it gives me output like this:

I want to extract the only "KeyPowerPurchasers" out of it which i am not able to do so.

I have been trying to get it in following these two methods but both are having problem:

first one:

local!purchaserKeys_int:reject(
fn!isnull,
{
if(
a!isNullOrEmpty(local!updatedPurchasers_at),
null,
cast(101, index(local!updatedPurchasers_at, "KeyPowerPurchasers", {}))
)
}
),

it gives empty array {}

2nd one:

  local!purchaserKeys_int:reject(
fn!isnull,
{
if(
a!isNullOrEmpty(local!updatedPurchasers_at),
null,
cast(101,a!fromJson(local!updatedPurchasers_at).KeyPowerPurchasers)
)
}
),

this gives 

Expression evaluation error at function a!fromJson : The jsonText parameter was not valid JSON.

can anyone please guide how can i get the list of KeyPowerPurchasers out of it only?

Best answer by harshitb6843

a!forEach(
  items: local!list,
  expression: extract(
    fv!item,"KeyPowerPurchasers:",","
  )
)

Save your output in a variable and pass that in place of local!list

8 replies

harshitb6843
June 9, 2023

a!forEach(
  items: local!list,
  expression: extract(
    fv!item,"KeyPowerPurchasers:",","
  )
)

Save your output in a variable and pass that in place of local!list

June 9, 2023

is it possible to map it into single list?

harshitb6843
June 9, 2023

How do you want your output to look like?

sunilc6392
June 9, 2023

mikes0011
Brainy
June 9, 2023

Do you have any control over how the data is formatted before being passed out from the process model?  It'd save you a lot of hassle in terms of parsing if you just wrap the data into a JSON string, instead of trying to do your own ad-hoc parsing.

June 9, 2023

You can also utilize the keval() function:

a!localVariables(
  local!list: {
    "[KeyPowerPurchaser:-2147483254,ExpectedPowerPurchased: 123]",
    "[KeyPowerPurchaser:-2147483253,ExpectedPowerPurchased: 456]"
  },
  local!result: a!forEach(
    items: local!list,
    expression: keyval(
      local!list[fv!index],  /* text  */
      "KeyPowerPurchaser",   /* keys  */
      ":",                   /* separtors  */
      ","                    /* delimiters  */
    )
  ),
  a!flatten(local!result)
)


The result will be: