skip to Main Content

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))"
            }
          }
        }
      }
    }
  }
]

2

Answers


  1. 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"
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. 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)"
                }
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search