4

I have a JSON

{
    "Id": "xxx",
    "Type": "Transaction.Create",
    "Payload": {
        "result": 2,
        "description": "Pending",
        "body": {
            "redirect": {
                "url": "xxx",
                "fields": {
                    "MD": "8a829449620619e80162252adeb66a39"
                }
            },
            "card": {
                "expiryMonth": "1",
                "expiryYear": "2033"
            },
            "order": {
                "amount": 1
            }
        }
    }
}

And I want to remove the card info of it like this:

{
    "Id": "xxx",
    "Type": "Transaction.Create",
    "Payload": {
        "result": 2,
        "description": "Pending",
        "body": {
            "redirect": {
                "url": "xxx",
                "fields": {
                    "MD": "8a829449620619e80162252adeb66a39"
                }
            },
            "order": {
                "amount": 1
            }
        }
    }
}

How can I do this with Apache velocity?

What works is:

#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.replaceAll("2033","2055"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}

This gives me

{
    "Id": "xxx",
    "Type": "Transaction.Create",
    "Payload": {
        "result": 2,
        "description": "Pending",
        "body": {
            "redirect": {
                "url": "xxx",
                "fields": {
                    "MD": "8a829449620619e80162252adeb66a39"
                }
            },
            "card": {
                "expiryMonth": "1",
                "expiryYear": "2050"
            },
            "order": {
                "amount": 1
            }
        }
    }
}

But now I want to remove the card part but it does not work:

#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.delete("$.Payload.body.card"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}

what am I doing wrong?

Main goal is transform a mapping template in API Gateway for a webhook. The webhook contains to many information and we want to remove some part of the JSON POST call.

DenCowboy
  • 13,884
  • 38
  • 114
  • 210
  • Hey, thats easy to do with other librarys, it must be with velocity? – BugsForBreakfast Sep 12 '19 at 12:57
  • Yes I think, I need to do this in a mapping template for API Gateway in AWS. So if it works there it's fine. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html – DenCowboy Sep 12 '19 at 13:14
  • Hey, did you manage to solve this? I have no idea about apache velocity, searched a bit but can't find like a remove or delete command, I think one option would be to create or build a new JSON (like with gson) with the information of these apache velocity json, but excluding the card, and then sending this new JSON without the card with apache velocity, you can do that? – BugsForBreakfast Sep 14 '19 at 16:55

1 Answers1

1

Try using the below

#set($dummy=$content.Payload.remove("card"))
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39