Home > Blockchain >  Combine shifted values and default values into a same sub object using Jolt
Combine shifted values and default values into a same sub object using Jolt

Time:10-15

Input:

{
  "banking_account": {
    "accounts": [
      {
        "accountId": "id1"
      },
      {
        "accountId": "id2"
      }
    ]
  }
}

Expected output:

{
  "Data": {
    "Accounts": [
      {
        "Account": {
          "Attribute1": "default",
          "Identification": "id1"
        }
      },
      {
        "Account": {
          "Attribute1": "default",
          "Identification": "id2"
        }
      }
    ]
  }
}

My current spec:

[
  {
    "operation": "shift",
    "spec": {
      "banking_account": {
        "accounts": {
          "*": {
            "accountId": "Data.Accounts.[&1].Account.Identification"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Attribute": "default"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "Accounts": {
          "*": {
            "*": "Data.Accounts.[&1].&",
            "@(3,Attribute)": "Data.Accounts.[&1].Account.Attribute1"
          }
        }
      }
    }
  }
]

Current output:

{
  "Data": {
    "Accounts": [
      {
        "Account": [
          {
            "Attribute1": "default"
          },
          {
            "Identification": "id1"
          }
        ]
      },
      {
        "Account": [
          {
            "Attribute1": "default"
          },
          {
            "Identification": "id2"
          }
        ]
      }
    ]
  }
}

It seems instead of inserting the key-value pair into the existing "Account" sub-object, it's making "Account" as a list, with one sub-object containing "Identification", and another object containing "Attribute1".

Could you help me understand why this happens, and how could I configure to avoid this?

Thanks a lot for your help in advance!

CodePudding user response:

Just prefixing the desired fixed value(default) with a # symbol would handle along with using a single shift transformation spec as in the following

[
  {
    "operation": "shift",
    "spec": {
      "banking_account": {
        "a*s": {
          "*": {
            "#default": "Data.A&(2,1)s[&1].A&(2,1).Attribute1", // "&(2,1)" represents going tree two levels up and grabbing the second piece(with index 1 which's after 0) from the literal(`accounts`) splitted by asterisk symbols
            "accountId": "Data.A&(2,1)s[&1].A&(2,1).Identification"
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related