Execute custom code with "Code step" action

Keragon’s Code Step is a powerful tool that allows you to write and execute custom logic using JQ expressions directly within your workflow. This makes it possible to transform and manipulate complex data structures without the need for external tools or services.

When to Use the Code Step

  • Extract or transform data from JSON objects.

  • Perform advanced filtering or conditional logic.

  • Generate calculated or aggregated values based on workflow data.

  • Clean and restructure data for downstream steps.

How to Use the Code Step Helper

1. Add the Code Step Action

  • Open your workflow editor.

  • Click the + icon to add a new step.

  • From the Helpers section, choose "Data Helpers".

  • Select the "Code Step" action.

2. Configure Input Variables

  • Add a Variable Name:

    • Give a clear, meaningful name to the input data you will reference in your code.

    • Example: If you’re processing patient phone numbers, name it contactNumbers.
      Click 'Add'

  • Select Data Type:

    • Choose the correct data type from the dropdown (e.g., String, Number, Object, Array).

    • Ensure this matches the actual data structure you are working with.Select the data type of your data. Make sure that exactly matches the data type that is available in the variable selector.

  • Map Data from Previous Steps:

    • Click the {} icon next to the input field.
      Click the {} icon to map it to previous step's data

    • Select the relevant data from previous steps using the variable selector.

3. Write Your Custom Code

  • Enter a valid JQ expression in the code editor.

  • Example Expression:

.contactNumbers | map(select(.type == "Home" or .type == "Mobile")) | map({(.type): .number}) | add
  • This example filters contact numbers to only include Home and Mobile types and restructures them as a flattened object.

💡 Tip: You can use ChatGPT to generate JQ expressions. Prompt Example:
"Act like a JQ expert. Based on the JSON below, create a JQ expression that extracts Home and Mobile phone numbers."

  • Proper Referencing:

    • Ensure your JQ expression starts by referencing the variable name you defined (e.g., .contactNumbers).
      Screenshot 2024-08-26 at 3.24.05 PM.png

4. Finalize and Test

  • Click "Save" to apply your code.

  • Click "Test Step" to execute the custom code.

  • Navigate to the Outputs tab to review the result.

Example Test Result:

{
  "Home": "123-456-7890",
  "Mobile": "987-654-3210"
}
Was this article helpful?
0 out of 0 found this helpful

Articles in this section

See more