Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jolt Spec for masking cards

How can I iterate through list and replace some elements in it? I have this input

{
  "clients": [
    {
      "clientId": "166734",
      "info": {
        "cards": [
          "378282246310005",
          "371449635398431"
        ]
      }
    }
  ]
}

What I expect cards will looks like this "cards" : [ "3782", "3714" ]

But in my spec do not work substring

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "cards": {
              "*": "=substring(@(1,&),0,@(1,4))"
            }
          }
        }
      }
    }
  }
]
like image 809
Alice Avatar asked Dec 29 '25 04:12

Alice


2 Answers

Note 1:

You should not use @(1,&) for getting an array index.

@(1,&) says: Go up 1 level (cards) and get 0 and 1 key from the array with &. But You have an array and should get the index from it.

You can say get & in an array like this: @(1,[&])

Note 2:

For getting the first 4 elements in the string you don't need the @(1,4). just say 4

Simpler solution:

We can get the current level without going up 1 level with @(0) or @0: @0,0,4

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "cards": {
              "*": "=substring(@0,0,4)"
            }
          }
        }
      }
    }
  }
]
like image 177
MohammadReza Avatar answered Jan 03 '26 17:01

MohammadReza


You can use shift transformations to tame the cards array in order to prepare for modify transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "cards": {
              "*": {
                "@": "&5[#5].&3.&2.&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "cards": {
              "*": "=substring(@(1,&),0,4)"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "cards": {
              "*": "&4[#4].&2.&1[#1]"
            }
          }
        }
      }
    }
  }
]

If there can always only exist two components for cards then we make the whole spec shorter by using [first/last]Element functions, otherwise use "=(@(1,cards[0/1]))" by individually writing such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "c0": "=firstElement(@(1,cards))", //or "=(@(1,cards[0]))"
            "c1": "=lastElement(@(1,cards))",  //or "=(@(1,cards[1]))"
            "cards0": "=substring(@(1,c0),0,4)",
            "cards1": "=substring(@(1,c1),0,4)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "*s*": "&3[#3].&1.&(0,1)s"
          }
        }
      }
    }
  }
]
like image 37
Barbaros Özhan Avatar answered Jan 03 '26 17:01

Barbaros Özhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!