Using collections from data action outputs in Architect

Hello -

I'm trying to use a data action to consume an API that returns a collection of objects. I need to reference specific elements of those collections within an Architect flow. I've looked at examples on how to do this, but can't seem to get it right.

The output of my API (for simplicity's sake) is:

[
  {
    "name": "name1",
    "description": "description1"
  },
  {
    "name": "name2",
    "description": "description2"
  },
]

My response config is:

{
  "translationMap": {
    "product": "$[*]"
  },
  "translationMapDefaults": {},
  "successTemplate": "{ \"products\" : ${product} }"
}

My output contract is:

{
  "type": "object",
  "properties": {
    "products.name": {
      "title": "names",
      "type": "array"
    },
    "products.description": {
      "title": "descriptions",
      "type": "array"
    }
  },
  "additionalProperties": true
}

I have "flatten input" turned on. When I test it, it returns:

{
  "products.name": [
    "name1",
    "name2"
  ],
  "products.description": [
    "description1",
    "description2"
  ]
}

Yet, when I use that data action within an Architect flow, and set the outputs to Task.names and Task.descriptions, then try to Play Audio on the expression ToAudioTTS(Task.names[0]), the flow quits unexpectedly.

Any help would be greatly appreciated. I can't tell if I'm close or miles away :slight_smile:

Hello,

If your API output is returning an array directly, I would use the following:

Output Contract:

{
  "type": "object",
  "properties": {
    "ProductNames": {
      "type": "array",
      "items": {
        "title": "Name",
        "type": "string"
      }
    },
    "ProductDescriptions": {
      "type": "array",
      "items": {
        "title": "Description",
        "type": "string"
      }
    }
  },
  "additionalProperties": true
}

Output Configuration:

{
  "translationMap": {
    "ProductNames": "$[*].name",
    "ProductDescriptions": "$[*].description"
  },
  "translationMapDefaults": {
    "ProductNames": "[]",
    "ProductDescriptions": "[]"
  },
  "successTemplate": "{\n   \"ProductNames\": ${ProductNames}\n, \"ProductDescriptions\": ${ProductDescriptions}\n}"
}

Task.names and Task.descriptions (if set on the Success Outputs ProductNames and ProductDescriptions) will be defined as collection of strings.

You can then use list functions (in the Expression Builder) to check their length and/or access a specific value - like the one of index 0 below - using:
GetAt(Task.names, 0)
GetAt(Task.descriptions, 0)

Regards,

Hi Adam,

The feedback that Jerome provided above seems sound, I just want to go through your original post to explain the issues that you were having.

A flow quitting unexpected often means you are trying to get a value from a null (unset) variable. Architect does have tools to check if a variable is set or not, so you can protect yourself from these sorts of situations.

You were actually pretty close to having this right, the problem is that you "hand flattened" the output contract, when Data Actions will take care of flattening the results as well as the output contract. The resulting double-flattening caused things to not work. In test mode the hint that something was wrong should have been a lack of results showing up in the nicer GUI at the top.

The flattener will flatten the results, no matter if it lines up with the output contract or not, so looking at the output of the data action can be really misleading. Because of this confusion I suggest that you make everything in your output contract required when you are creating it. That will cause the data action to fail if everything isn't where you expect it to be. With nothing required a data action can "work", but not really return anything useful.

To make your original approach work you would need to have your output contract be an array of objects that contained name and description strings. If you want to take that approach or Jerome's approach is up to you.

--Jason

Thanks much Jerome & Jason! This was super helpful.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.