Reliable parsing and querying of JSON in OnBeforeResponse

1,275 views
Skip to first unread message

K1978

unread,
Dec 4, 2014, 12:18:12 PM12/4/14
to httpf...@googlegroups.com
Hi!

I have looked around the Internet for quite some time. But I cannot find any API documentation for Fiddler.WebFormats.JSON. In the documentation I was able to download Fiddler was included but not Fiddler.WebFormats.

In certain requests I get JSON data in the response body. The data looks something similar to this:

{"params":{"key1":"46","key2":"12","key3":"0","key4":"100,500", ... }

At them moment I am parsing it using:

        var oResponseBody = oSession.GetResponseBodyAsString();
       
        if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "application/json")) {
            var oJSON = Fiddler.WebFormats.JSON.JsonDecode(oResponseBody);
            // more code and error handling should follow here
        }   

My concrete questions are:
  1. Is there any API documentation available and for Fiddler.WebFormats.JSON and where can I find it?
  2. Should I use something else to parse into JSON? I tried import JSON at the top of the script and then JSON.parse(...), but that did not work for me.
  3. How can I detect if oResponseBody could not be parsed to JSON?
  4. How do I loop over the keys in the structure and log key = value?

In the long run. It would be nice if I could create a plug-in which would allow the user to add custom columns that displays certain JSON paths. So that the user could enter an xpath like /params/username and tell Fiddler to display that in a column named Username. That plug-in should also make it possible to load and save sets of these custom columns... Anyway, I am a long way from being able to do that. :-)


EricLaw

unread,
Dec 4, 2014, 2:41:19 PM12/4/14
to httpf...@googlegroups.com
https://groups.google.com/forum/#!searchin/httpfiddler/webformats$20json/httpfiddler/K70HvmP3xgY/DloDBGxswNEJ includes a simple example of how to use the WebFormats.JSON class, but you probably would want to use something like NewtonSoft or another library if you're doing anything non-trivial.

For your code, you definitely don't want to waste time getting the body as a string if it's not JSON. So you'd do something like:


if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "application/json"))
{

   var oJSON: Fiddler.WebFormats.JSON.JSONParseResult;
   oJSON = (JSONParseResult) Fiddler.WebFormats.JSON.JsonDecode(oSession.GetResponseBodyAsString());
   var oParams = oJSON.JSONObject[0]["params"];
}

The root JSONObject and its children are one of the following types: Hashtable, ArrayList, double, string, or null. So in your example, you'd have a hashtable named params, which contains your key/value pairs.

  

Outty Rocks

unread,
Dec 16, 2015, 11:42:47 AM12/16/15
to Fiddler
Eric is there a way to parse the JSON in Fiddler Core also?

I can get the request or response just rather not use split

.

Just I'd rather then split the JSON string by params then using split.


string strResponseBody = System.Text.Encoding.UTF8.GetString(session.ResponseBody);


by param be better then using split is method similiar then this

string[] split = strRequestBody.Split(new Char[] { ' ', ',', '.', ':', '\t' }); //example of split method

EricLaw

unread,
Dec 16, 2015, 5:34:05 PM12/16/15
to Fiddler
I'm not entirely sure what you're asking. You should get the response body by calling oSession.GetResponseBodyAsString() rather than assuming that the body is in any particular encoding. You can then parse that string using any of the many JSON decoders available; the Newtonsoft JSON.NET Library is popular, for instance.
Reply all
Reply to author
Forward
0 new messages