Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Velocity: remove key/value from json

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.

like image 245
DenCowboy Avatar asked Nov 28 '25 08:11

DenCowboy


1 Answers

Try using the below

#set($dummy=$content.Payload.remove("card"))
like image 170
Prerana Sharma Avatar answered Nov 30 '25 21:11

Prerana Sharma