Manipulating specific response body values w/o using manual response action.

1 Answer 106 Views
Fiddler Everywhere
Peter
Top achievements
Rank 1
Peter asked on 03 Jan 2023, 01:49 PM | edited on 03 Jan 2023, 01:53 PM

I have a general question towards setting a value of a response.

Imagine the following response body:


{
    "mobile-getjackpots-response": {
        "errorcodes": [
            "420"
        ],
        "jackpots": [
            {
                "gametype": "1",
                "jackpotamount": "1000000",
                "nextDrawDay": "thursday"
            }
        ]
    }
}

I want to create a rule to set ONLY the value of "jackpotamount" to a different value and I want it to do the change irrespectively of the value it currently has or the structure of the response body. I DO NOT want to constantly adjust the response via the manual response action. This creates too much overhead when the response body is larger.

This is want I want the response to look like.

{
    "mobile-getjackpots-response": {
        "errorcodes": [
            "420"
        ],
        "jackpots": [
            {
                "gametype": "1",
                "jackpotamount": "100",
                "nextDrawDay": "thursday"
            }
        ]
    }
}

 

I tried a find and replace rule shown in the attachment, but it resulted in this:

{
    "mobile-getjackpots-response": {
        "errorcodes": [
            "420"
        ],
        "jackpots": [
            {
                "gametype": "1",
                "jackpotamount": "100": "1000000",
                "nextDrawDay": "thursday"
            }
        ]
    }
}

I know that I should´ve used "jackpotamount": "1000000" for this to work, but this is exactly what I try to avoid, as I dont know beforehand, what the value will be that I want to change. 

I also tried it with the following regex, it should theoretically match the key-value-pair "jackpotamount": "SOME_AMOUNT"
https://regex101.com/r/u8r4IZ/1

(?:\"jackpotamount)(?:\"\s?:\s?\")(.*)(?:\")

This "cut off" the rest of the first jackpots object.

{
    "mobile-getjackpots-response": {
        "errorcodes": [
            "420"
        ],
        "jackpots": [
            {
                "gametype": "1",
                "jackpotamount": "100"
            }
        ]
    }
}

1 Answer, 1 is accepted

Sort by
1
Accepted
Rosen Vladimirov
Telerik team
answered on 04 Jan 2023, 02:01 PM | edited on 04 Jan 2023, 02:02 PM

Hi Peter,

To achieve the desired functionality you have to use Regular Expression as you've already tried. I think the problem with your regular expression is that you are testing it with the formatted version of the JSON. Usually, when JSON is sent with HTTP response, it is a single line. Fiddler Everywhere formats it when showing it in the inspectors, to improve the readability. 
So, when I tried using your response body with the regular expression you've used, I noticed you are using a greedy RegExp - you have .* in it, which tries to match as much symbols as possible. After changing this part of the regular expression to a non-greedy one (.*?) everything seems to work as expected.
So, I suggest you to try using the following Regular expression: (?:\"jackpotamount)(?:\"\s?:\s?\")(.*?)(?:\") and then you should be able to replace the value of jackpotamount.

Regards,
Rosen Vladimirov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Peter
Top achievements
Rank 1
commented on 04 Jan 2023, 04:03 PM

Hi Rosen.

That worked like a charm, thank you very much.

 

Tags
Fiddler Everywhere
Asked by
Peter
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
Share this question
or