Working with Arrays

Marshalling (writing) and unmarshalling (reading) is used both on the client / sender (upstream) and server / receiver (downstream) side of the GLU engine.

When a request enters the GLU engine, it undergoes unmarshalling (reading) to transform it into the GLU Object Model (JSON). This can occur with either an inbound request or an outbound response. Unmarshalling can involve saving an identical array, searching for specific values, or storing only a portion of the array’s contents.

Note: See the response template details for how to include the array in the response. Response Manager

Unmarshalling like for like arrays

If my response from a third party looked like this:

{“result”: [
{
“accountID”: 0,
“amount”: 17209,
“balanceType”: “Savings”
},
{
“accountID”: 1,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 23,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 65,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 1018,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 1018,
“amount”: 0,
“balanceType”: “Money”
}
]}

For each parameter in the array, the unmarshalling and parameter properties would be defined as follows:

  • Unmarshalling: In this step, the object/collection path is specified to define the path where to locate the array, along with the attribute inside it that will be saved into the parameter.
  • Properties: In this section, the parameter name is specified along with the object (array name) in which the parameter will be saved.

Unmarshalling from unnamed arrays

If my response from a third party looked like this:

[
{
“veggieName”: “potato”,
“veggieLike”: true
},
{
“veggieName”: “broccoli”,
“veggieLike”: false
}
]

You can follow the above steps and replace the name of the array with empty square brackets – as shown in the ‘Object / Collection Path’ field below:

Marshalling to unnamed arrays

If you want to marshal an array and save it as an unnamed array, you can use the ‘Store as Object’ option. This way, the array will be stored as an object (unnamed array). Here’s an example:

{
“array”: [
{
“attribute”: “value1”
},
{
“attribute”: “value1”
}
]
}

To:

[
{
“attribute”: “value1”
},
{
“attribute”: “value1”
}
]

Unmarshalling/Marshalling array of values

If the response form third party looks like this:

{
arrayOfValues”:[16,6,2,8]
}

You can follow the above steps, but leave the attributes fields empty to unmarshall or to marshall.

Unmarshalling using a condition

If my response from a third party looked like this:

{“result”: [
{
“accountID”: 0,
“amount”: 17209,
“balanceType”: “Savings”
},
{
“accountID”: 1,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 23,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 65,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 1018,
“amount”: 0,
“balanceType”: “Money”
},
{
“accountID”: 1018,
“amount”: 0,
“balanceType”: “Money”
}
]

}

If you want to only find the balance on accountID = 0, you can add a condition to your unmarshalling configuration. This condition must be met within one of the records within the array. Since you expect the result set to have only one instance of this condition being met, you can save this to a single parameter.

However, if there are multiple instances of the condition being met, you can still save it to the parameter, but you also need to include the object (array) name in the property object field.

Unmarshalling only part of the array contents

In the above example, if you only want to unmarshal the array and save the accountID and amount fields into an array, then you only need to add those two parameters in the response parameters with the correct configuration included for unmarshalling. This will create the new accounts object without the balanceType parameter.

Consolidating amounts in an array

If I received an array with multiple records for one unique identifier (e.g. accountID) and you wanted to return a consolidated / overall balance for each accountID you could use the following function. This function could be executed on a response handler once the array has been unmarshalled into the engine. It is available in GLU Functions & Formulas

If you receive an array with multiple records for one unique identifier (e.g., accountID) and you want to return a consolidated/overall balance for each accountID, you can use the following function, available in GLU Functions & Formulas:

CONSOLIDATE(${accounts},accountID,amount)

  • parameter 1 (arrayName): the array name where you have unmarshalled your data
  • parameter 2 (paramNameConsolidateBy): consolidate to this identifier
  • parameter 3 (paramNameToConsolidate): the parameter which you want to add up

This function would work for an incoming array structured like this:

{“result”: [
{
“accountID”: 65,
“amount”: 0
},
{
“accountID”: 1018,
“amount”: 10
},
{
“accountID”: 1018,
“amount”: 20
}
]

}

The output would look like this:

{“accounts”: [
{
“accountID”: 65,
“amount”: 0
},
{
“accountID”: 1018,
“amount”: 30
}
]

}


In your configuration, you would first set a derived parameter. As part of your response parameters, you would unmarshal the parameters that you require into an accounts array. Then on your response handler, you can use a ‘none’ handler to overwrite your derived parameter with your function. See the example below:

Unmarshalling a specific position in an array

You can access a specific position in an array by using a shortcut:

{“accounts”: [
{
“accountID”: 65,
“amount”: 0
},
{
“accountID”: 1018,
“amount”: 30
}
]

}

In the accounts array, to retrieve the value of the accountID from the first record, you can use the following expression:

${accounts[0].accountID}
  • accounts: Refers to the array name.
  • [0]: Indicates the position in the array (in this case, the first record).
  • accountID: Specifies the value in the first record that you want to retrieve.
Was this article helpful?

Related Articles

Fill the form and we’ll contact you shortly

    I agree with

    cookies
    We uses cookies to make your experience on this website better. Learn more
    Accept cookies