bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Having issue when adding a common value to every elements in array using Jolt #1169

Closed yesluck closed 1 year ago

yesluck commented 1 year ago

Hi Team,

I am currently using Jolt to work on this input:

{
    "banking_account": {
        "accounts": [
            {
                "accountId": "account1"
            },
            {
                "accountId": "account2",
                "nickname": "nickname2"
            }
        ]
    },

    "account_balance": {
        "total_value": {
            "currency_code": "USD",
            "value": "100.00"
        },
        "balance_accounts": [
            {
                "id": "id1"
            },
            {
                "id": "id2"
            }
        ]
    }
}

And the expected output is:

{
    "Data": {
        "Accounts": [
            {
                "AccountId": "account1",
                "Account": {
                    "Identification": [
                        "id1",
                        "id2"
                    ]
                },
                "Currency": "USD"
            },
            {
                "AccountId": "account2",
                "NickName": "nickname2",
                "Account": {
                    "Identification": [
                        "id1",
                        "id2"
                    ]
                },
                "Currency": "USD"
            }
        ]
    }
}

And here is my current spec:

[
    {
        "operation": "shift",
        "spec": {
            "banking_account": {
                "accounts": {
                    "*": {
                        "accountId": "Data.Accounts.[&1].AccountId",
                        "nickname": "Data.Accounts.[&1].NickName"
                    }
                }
            },
            "account_balance": {
                "total_value": {
                    "currency_code": "Data.Accounts.[&1].Currency"
                },
                "balance_accounts": {
                    "*": {
                        "id": "Data.Accounts.[&1].Account.Identification"
                    }
                }
            }
        }
    }
]

And here is my current output:

{
    "Data": {
        "Accounts": [
            {
                "AccountId": "account1",
                "Account": {
                    "Identification": "id1"
                }
            },
            {
                "AccountId": "account2",
                "NickName": "nickname2",
                "Account": {
                    "Identification": "id2"
                }
            }
        ]
    }
}

So I am having two questions:

  1. Why is the "Currency" value missing? Even if I added something like

    "account_balance": {
    "total_value": {
        "currency_code": "Data.Accounts.[&1].Currency"
    }
    }
  2. How to map both two balances accounts to both two accounts? Now it seems like an 1->1 mapping; and I wish to have a 2*2 mapping.

Thanks a lot for your help in advance!

yesluck commented 1 year ago

Resolved in https://stackoverflow.com/questions/74053653/having-issue-when-adding-a-common-value-to-every-elements-in-array-using-jolt