Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to write a FiddlerScript to modify the properties of a JSON array that is returned from the server.

Here is what I have tried so far:

static function OnBeforeResponse(oSession: Session) {
    if (m_Hide304s && oSession.responseCode == 304) {
        oSession["ui-hide"] = "true";
    if(oSession.HostnameIs("myserver.com") && oSession.uriContains("info")) {
        oSession["ui-backcolor"] = "lime";
        // Convert the request body into a string
        var oBody = System.Text.Encoding.UTF8.GetString(oSession.requestBodyBytes);
        // Convert the text into a JSON object
        var j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
        var placementsArray = j.JSONObject["placements"];
        FiddlerObject.log("Got Placements.");
        // I can't figure out how to access the elements of the array
        //for (var i: int=0; i < placementsArray.Length; i++) {
        //    placements[i]["isValid"] = true;
        // Convert back to a byte array
        var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
        // Convert json to bytes, storing the bytes in request body
        var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
        oSession.RequestBody = mod;

I need help with the for loop that is commented out in the middle of this function. I would like to walk the array of "placements", and then change a value called "IsValid" in each of these "placement" objects. I need to do this so that I can modify responses from a server, so that I can test a client app with different server responses for the array item property values.

Here is the answer in case it helps anyone. I was incorrectly getting and setting the Request body instead of the Response body, and I was also using the non-existent "Length" property of ArrayList instead of "Count".

static function OnBeforeResponse(oSession: Session) {
    // This code was already here, leaving it
    if (m_Hide304s && oSession.responseCode == 304) {
        oSession["ui-hide"] = "true";
    // Here is new code to modify server's response
    if(oSession.HostnameIs("myserver.com") && oSession.uriContains("info")) {
        // Color this response, so we can spot it in Fiddler
        oSession["ui-backcolor"] = "lime";
        // Convert the request body into a string
        var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
        var j: Fiddler.WebFormats.JSON.JSONParseResult;
        // Convert the text into a JSON object
        // In this case our JSON root element is a dictionary (HashTable)
        j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
        // Inside of our dictionary, we have an array (ArrayList) called "placements"
        var placementsArray = j.JSONObject["placements"];
        for (var iEach = 0; iEach < placementsArray.Count; iEach++){
            // In each object/member in the array, we change one of its properties
            placementsArray[iEach]["isValid"] = true;
        // Convert back to a byte array
        var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
        // Convert json to bytes, storing the bytes in request body
        var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
        oSession.ResponseBody = mod;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.