Skyone
Skyone
English
English
  • Home
  • Data
    • Getting Started
      • Create an account
      • Recover Password
      • Quick Platform Guide
      • How to test the platform for free
      • Workspace
        • Creating a new Workspace
        • Find a Workspace
        • Sending an invitation to a Workspace
        • Editing a Workspace
      • Organizations
        • Creating an Organization
        • Organization Overview
        • Organization Management
        • Organization Monitoring
      • Settings and Preferences
        • Profile
        • Notifications
        • Usage and Billing
        • Users and Permissions
    • Modules
      • Module management
        • Creating a Module
        • Importing a Module
          • IAC Files - Integration as Code
        • Editing a module
        • Module Options
      • Settings and Operations
        • Module settings
          • Connectivity: Database
          • Connectivity: Email
          • Connectivity: REST
          • Connectivity: SOAP
          • Connectivity: File
          • Connectivity: RFC
          • Connected Account Management
        • Operations
          • Importing operations into REST Modules
          • Operation Management
        • Flows Using This Module
    • Monitoring
    • API Gateway
    • Terminals & Agent
      • Agent
        • Versions supported by Agent
        • How to Update the Agent Version
        • How to back up Agent files
      • Terminals
    • Data
      • Data Stack
        • Process Control
        • Data Stack Upload
        • File Actions
        • File Jobs
        • Data Job Parameters
        • Data Store
        • Data Share Features
        • ODBC
        • How to use the Data Engine Proxy
    • Integrations
      • Integration Management
        • Create integration
        • Import Integration
        • Edit Integration
        • Integration Options
        • Flows of this integration
      • Flows
        • Flow management
          • Creating a flow
          • Flow options
          • Flow Canva: configuring and editing the flow
            • Flow Canva: overview
            • Exception Handler
              • Exception Handler - Configuration
              • Exception Handler - Cases
            • Multicontext Flows
              • Example: Multicontext with an API Gateway
              • Example: Multicontext with a Time Trigger
            • Flow Settings
        • Triggers of a flow
          • API Gateway Triggers: Adding and Setting
          • AS2 Triggers: Adding and Setting
          • Queue Triggers: Adding and Setting
          • Flow Triggers: Adding and Setting
          • Time Triggers: Adding and Setting
          • Webhook Triggers: Adding and Setting
        • Tool Modules
          • AS2 Module
          • CSV Module
          • Data Transform Module
          • Data Balancer Module
          • EDI Module
          • Flow Call Module
          • IF Module
          • JavaScript Module
          • Log Module
          • Loop Do While Module
          • Loop For Module
          • Return Module
          • XML Module
          • Other Tool Modules
        • Module Header
        • Connecting components of a flow
        • Editing triggers and modules
        • Data Operations
          • Object Handling
            • Practical example: Handling variables
          • SMOP (Small Operations)
          • Parameterization rules
    • How to
      • Insert JSON into databases
      • Flattening: Data transformation using JSONata
      • How to use Form Data
      • Understanding recursion in JSONata
      • REST Module Output Consolidation
      • Isolated in execution: concept and application in variables
      • URL Parameters in API Gateway
      • Use case: API Gateway trigger parameters
      • Use case: Exception Handler in financial transactions
      • Use case: using Groups to manage access to flows
      • How to create a download endpoint and integrate with Power BI
      • Is it possible to use two triggers in a single flow?
      • How to set up WhatsApp in Skyone Studio
    • FAQ
    • GIGS: The complete guide
    • Glossary
  • Support
    • How do I request support?
    • Case Severity Levels
    • SLAs
    • Help & Resources
Powered by GitBook
On this page
  • Explanation
  • 1. Function Definition:
  • 2. Applying the Function
  • Enhancing the Example
  • Additional Example & Use Cases
  • Conclusion
  1. Data
  2. How to

Understanding recursion in JSONata

PreviousHow to use Form DataNextREST Module Output Consolidation

Last updated 11 months ago

Next, we'll present an example that defines recursion to navigate and transform a JSON structure. This article will detail the initial example and expand it with additional explanations, practical examples and use cases, providing a comprehensive understanding of the application of recursion in JSONata.

(
   $person := function($node) { $node.{"n": Name, "p":$person(Parent.Person)}};


   {
       "Person": Person ~> $person($)
   }
)

Explanation

1. Function Definition:

$person := function($node) { $node.{"n": Name, "p":$person(Parent.Person)}};

The line above defines a recursive function called $person. The function takes a node ($node) as an argument. It returns an object containing:

  • "n": Name - The Name property of the current node.

  • "p": $person(Parent.Person) - The result of calling the $person function on the Parent.Person property of the current node, allowing for recursive navigation.

2. Applying the Function

 {
    "Person": Person ~> $person($)
}

This part applies the $person function to the "Person" field in the current context (denoted by $). The ~> operator pipes the "Person" field through the $person function.

Enhancing the Example

Let's enhance the example by adding more fields for transformation and navigating through more deeply nested JSON structures. Consider the following more complex JSON structure:

JSON Input Example

{
 "Person": {
   "Name": "João",
   "Age": 30,
   "Parent": {
     "Person": {
       "Name": "Maria",
       "Age": 55,
       "Parent": {
         "Person": {
           "Name": "Roberto",
           "Age": 80
         }
       }
     }
   }
 }
}

Expanded Function and Query

Let's modify the function to include the "Age" property and handle cases where "Parent" may be absent.

(
   $person := function($node) {
   $node.{
           "n": Name,
           "a": Age,
           "p": $exists($person(Parent.Person)) ? $person(Parent.Person) : null
       }
   };


   {
       "Person": Person ~> $person($)
   }
)

Explanation

  • The modified function now includes "a": Age to capture each person's age.

  • The ternary operator ($exists($person(Parent.Person)) ? ... : null) ensures the function returns "null" if the node is not present, preventing errors when navigating the hierarchy.

Example Output

Applying the expanded query to the input JSON example will result in:

{
"Person": {
  "n": "João",
  "a": 30,
  "p": {
    "n": "Maria",
    "a": 55,
    "p": {
      "n": "Roberto",
      "a": 80,
      "p": null
    }
  }
}
}

Additional Example & Use Cases

Example with Siblings

Let's consider an example where each person can have siblings.

JSON Input

{
 "Person": {
   "Name": "João",
   "Age": 30,
   "Siblings": [
     {
       "Name": "Ana",
       "Age": 25
     },
     {
       "Name": "Alex",
       "Age": 28
     }
   ],
   "Parent": {
     "Person": {
       "Name": "Maria",
       "Age": 55,
       "Parent": {
         "Person": {
           "Name": "Roberto",
           "Age": 80
         }
       }
     }
   }
 }
}

Expanded Function and Query to Handle Siblings

We can expand our role even further to include brothers:

(
   $person := function($node) {
   $node.{
           "n": Name,
           "a": Age,
           "s": $exists($person(Siblings)) ? $person(Siblings) : null,
           "p": $exists($person(Parent.Person)) ? $person(Parent.Person) : null
       }
   };


   {
       "Person": Person ~> $person($)
   }
)

Explanation

  • The line "s": $exists($person(Siblings)) ? $person(Siblings) : null maps the Siblings array and transforms each sibling into an object.

Example Output

Applying the expanded query to the input JSON example will result in:

{
 "Person": {
   "n": "João",
   "a": 30,
   "s": [
     {
       "n": "Ana",
       "a": 25,
       "s": null,
       "p": null
     },
     {
       "n": "Alex",
       "a": 28,
       "s": null,
       "p": null
     }
   ],
   "p": {
     "n": "Maria",
     "a": 55,
     "s": null,
     "p": {
       "n": "Roberto",
       "a": 80,
       "s": null,
       "p": null
     }
   }
 }
}

Conclusion

The recursive function in JSONata provides a powerful way to navigate and transform nested JSON structures. By defining and applying such functions, you can traverse hierarchical data, handle optional fields and even map arrays to produce complex, automated transformations.

Explanation
Function Definition
Applying the Function
Incrementing the Example
JSON Input Example
Expanded Function and Query
Explanation
Example output
Examples and Use Cases
Example with Siblings
JSON Input
Expanded Function and Query to Handle Siblings
Conclusion