cancel
Showing results for 
Search instead for 
Did you mean: 

Splitting Json with array[0]

neelamanikandan
New Contributor II

I have a Json data, that contains a field that could be an array as per Source definition. Json given below

It is not always mandatory that the FeeReductionDetail will always be an array with multiple elements, sometimes it could be with just one element i.e., array [0].

I use a Json splitter with JSON Path to Split* as FeeReductionDetail to flatten out the array and convert the above Json into two table rows, this work when the Split path contains a real list of values, but when there is only one element it fails. 

Json splitter Sucess : Json example

{
    "VehicleId": "_Id4",
    "FeeReduction": {
        "FeeReductionDetail": [
            {
                "Type": "Type10",
                "ExpirationDate": "2023-08-03",
                "ManagementFee": "2.2",
                "ExpenseRatio": "8.3",
                "DividendExpense": "1.0"
            },
            {
                "Type": "Type21",
                "ExpirationDate": "2023-08-03",
                "ManagementFee": "4.3"
            }
        ]
    },
    "SwitchingFee": "4.1",
    "TransactionFee": "6.1",
    "MaxManagementFee": "1.1",
    "ActualManagementFee": "8.4",
    "ProspectusDate": "2023-08-03",
    "CtgyId": "Id315"
}.

 

Json Splitter Failure : Json example
{
    "VehicleId": "_Id4",
    "FeeReduction": {
        "FeeReductionDetail": 
            {
                "Type": "Type10",
                "ExpirationDate": "2023-08-03",
                "ManagementFee": "2.2"
            }
    },
    "SwitchingFee": "4.1",
    "TransactionFee": "6.1",
    "MaxManagementFee": "1.1",
    "ActualManagementFee": "8.4",
    "ProspectusDate": "2023-08-03",
    "CtgyId": "Id315"
}.

1 ACCEPTED SOLUTION

Aleksandar_A
Contributor III

Hello @neelamanikandan,

You can use the sl.ensureArray() function to ensure that the FeeReductionDetail field will always be an array.

Just place a Mapper before the Splitter with the following settings:

Expression: sl.ensureArray($FeeReduction.FeeReductionDetail)

Target Path: $FeeReduction.FeeReductionDetail

With Pass through turned on.

Let me know if this helps you.

Regards,

Aleksandar.

View solution in original post

2 REPLIES 2

Aleksandar_A
Contributor III

Hello @neelamanikandan,

You can use the sl.ensureArray() function to ensure that the FeeReductionDetail field will always be an array.

Just place a Mapper before the Splitter with the following settings:

Expression: sl.ensureArray($FeeReduction.FeeReductionDetail)

Target Path: $FeeReduction.FeeReductionDetail

With Pass through turned on.

Let me know if this helps you.

Regards,

Aleksandar.

neelamanikandan
New Contributor II

Thanks for the quick response. This helped!