I have a JSON string and I want to parse it to an object (to do some query after that).
I have found Fiddler.WebFormats.JSON.JsonDecode() but the returned JSONObject has no method to get or set content.
How to decode and use JSON strings ?
Thanks for any help !
1 Answer, 1 is accepted
The returned object has a JSONObject property which is an ArrayList, Hashtable, or primitive value that is equivalent to the json object in question. Fiddler uses this class in its JSONView extension.
If you prefer a different pattern (e.g. something with strong typing, a schema, etc) then you can & should use one of the myriad JSON encoder/decoders available for the .NET Framework.
Regards,
Eric Lawrence
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Thanks for your quick answer. My wrong was to miss JSONObject and its meaning.
I post my test case (in FiddlerScript) in case someone has the same question:
var
sInput =
'{"a" : 1, "b" : [2, 3, 4]}'
;
var
oJSON = Fiddler.WebFormats.JSON.JsonDecode(sInput);
FiddlerApplication.Log.LogFormat(
'input: {0}'
, sInput);
FiddlerApplication.Log.LogFormat(
'oJSON: {0}'
, oJSON.ToString());
FiddlerApplication.Log.LogFormat(
'oJSON["a"]: {0} (expected: 1)'
, oJSON.JSONObject[
"a"
]);
FiddlerApplication.Log.LogFormat(
'oJSON["b"] : {0} (expected: 2)'
, oJSON.JSONObject[
"b"
][0]);
Hashtable ht = (Hashtable)json.JSONObject;
string value= ht["name"].ToString();
Hi
I am trying to parse a simple JSON using Fiddler Script.
var sInput = '{"a" : 1, "b" : [2, 3, 4]}';
var json = Fiddler.WebFormats.JSON.JsonDecode(sInput);
But from the intellisense json.JSONObject does not showup. If I type on my own it throws error. This may be very basic, but I am not able to get fwd. Please help.
Thanks
Hi Vittal,
This is because FiddlerScript is built using Jscript.NET with loose type checking. Although, it appears that the json variable is the JSONObject. Are you able to Log the output? Try the following code snippet.
var sInput = '{"a" : 1, "b" : [2, 3, 4]}';
var json = Fiddler.WebFormats.JSON.JsonDecode(sInput);
FiddlerApplication.Log(json.ToString());
Please give this a try and let me know the results. Thank you and I look forward to your reply.
Regards,
Eric R | Technical Support Engineer
Progress Telerik
Hello, I'm trying to use almost the same commands, and I'm stuck :
This is what the webpage json looks like :
[
{
"responseData": true,
"requestId": 126
},
{
"responseData": false,
"requestId": 127
}
]
I'm trying to save the whole content of the LAST node in a separate file. With the given example, I'd like to save 1 file : request_127.json
Here's my actual code :
var
sInput = oSession.GetResponseBodyAsString ;
var
oJSON = Fiddler.WebFormats.JSON.JsonDecode(sInput);
Log.LogFormat(oJSON.JSONObject[0][
"requestId"
]);
In the log tab of Fiddler I get the message "udefined" ...
I'm sure that I'm pretty close to a solution, but I can't figure what I've done wrong ...
Please help me ... (event with just a link to documentation for the commands I'm using ...)
Other articles already used :
https://feedback.telerik.com/fiddler/1361506-how-to-call-json-methods-inside-fiddler-script
https://www.telerik.com/forums/how-to-traversal-a-array-in-json-response-in-customrules-js
Hello Oudin,
I've tried the you code on my side and it is working as expected. Here is a sample:
var sInput = '[{ "responseData": true,"requestId": 126 },{"responseData": false, "requestId": 127 }]'; var oJSON = Fiddler.WebFormats.JSON.JsonDecode(sInput); FiddlerApplication.Log.LogFormat('>>>>>>>>>>>>>>>>>> input: {0}', sInput); FiddlerApplication.Log.LogFormat('>>>>>>>>>>>>>>>>>> oJSON: {0}', oJSON.ToString()); FiddlerApplication.Log.LogFormat(oJSON.JSONObject[0]["requestId"]); // outputs 126
Check if the undefined errors are related to the content of oSession.GetResponseBodyAsStrin.
Regards,
Nick Iliev
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hello Nick, and first of all : Thanks for your time !
I think the oSession object is fine, I'm saving it on the hard drive (to check content ...) :
oSession.SaveResponseBody(FilePath)
And the content of the file is normal, regular json ...
I tried to reuse your code sample AS-IS, and it worked. Here's the output :
10:34:13:2962 >>>>>>>>>>>>>>>>>> input: [{ "responseData": true,"requestId": 126 },{"responseData": false, "requestId": 127 }]
10:34:13:2962 >>>>>>>>>>>>>>>>>> oJSON: Fiddler.WebFormats.JSON+JSONParseResult
10:34:13:2962 126
The 2nd line seems a bit weird to me ??? but it works !
Then I replaced your line (that is recreating the json content as a string):
var sInput = '[{ "responseData": true,"requestId": 126 },{"responseData": false, "requestId": 127 }]';
By the line I'm intending to use (the one that gets the json from the website) :
var sInput = oSession.GetResponseBodyAsString;
And I got this ouput (undefined) :
10:37:21:1364 >>>>>>>>>>>>>>>>>> input: System.String GetResponseBodyAsString()
10:37:21:1364 >>>>>>>>>>>>>>>>>> oJSON: Fiddler.WebFormats.JSON+JSONParseResult
10:37:21:1364 undefined
I'm getting crazy here ^^ specially because I can't figure why there is a difference between a json created by hand from a json downloaded from a webserver ...
Any ideas ?
Hi Oudin,
It looks like this needs to be cast to a hashtable since the included JSONObject is not optimized for FiddlerScript. See the following steps to test this.
Testing
First, in the Custom Rules OnBeforeResponse event handler try the following code snippet.
public static void OnBeforeResponse(Session oSession)
{
if (m_Hide304s && oSession.responseCode == 304)
{
oSession["ui-hide"] = "true";
}
if(oSession.HostnameIs("www.httpbin.org") && oSession.uriContains("post"))
{
oSession["ui-backcolot"] = "yellow";
}
string sInput = oSession.GetResponseBodyAsString();
JSON.JSONParseResult json = (JSON.JSONParseResult)JSON.JsonDecode(sInput);
Hashtable oObj = (Hashtable)json.JSONObject;
string value = (oObj != null) ? (string)oObj["url"] : "null";
FiddlerApplication.Log.LogString(value);
}
Then, use the Composer and POST a custom request to httpbin.org which will respond with JSON. See the below screenshot.
Finally, in the log this will output the url value submitted through the composer. See the below screenshot.
Note that this will only work with the httpbin.org because it returns a url value in the JSON Response.
Wrapping Up
For additional reference, see Eric's response in the How to call JSON methods inside FiddlerScript feature request.
Please give this a try and let me know if you need any additional information. Thank you for using the Fiddler Forums.
Regards,
Eric R | Senior Technical Support Engineer
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hello Eric,
I'm starting to wonder if I'm not using a wrong language or something ...
I tried your code as is, after removed my own OnBeforeResponse procedure, and I get this error message when I try it :
Compiler Error on line 8 : ";" or ")" expected
Your procedure declaration looks like this :
public static void OnBeforeResponse(Session oSession)
where mine looks like this :
static function OnBeforeResponseBackup(oSession: Session)
Am I using Jscript where I should use C, or something like this ?
Hi Oudin,
My apologies for the confusion. The previous code example is using the C# version of the Custom Rules file. This can be changed in the Scripting Tab of the Options dialog as shown in the below screenshot.
Alternatively, below is the JScript.NET version of the code.
var sInput = oSession.GetResponseBodyAsString();
var json:JSON.JSONParseResult = JSON.JsonDecode(sInput);
var oObj = json.JSONObject;
FiddlerApplication.Log.LogString(oObj["url"]);
Please give this a try and let me know if you need any additional information. Thank you
Regards,
Eric R | Senior Technical Support Engineer
Progress Telerik
Hello Eric,
apologies for the late answer ... it's holiday time here ...
I sticked to Jscript because I find it easier to read afterwards ...
I tried your code snippet, and I still get this in the log window :
Jscript Code :
var sInput = oSession.GetResponseBodyAsString;
var json = Fiddler.WebFormats.JSON.JsonDecode(sInput);
var oObj = json.JSONObject;
FiddlerApplication.Log.LogString(oSession.url);
FiddlerApplication.Log.LogString(oObj["url"]);
And here's the output log :
21:58:29:5661 www.httpbin.org/post?id=123&ware=were
21:58:29:5661 undefined
21:58:37:0870 safebrowsing.googleapis.com:443
21:58:37:0870 undefined
I got this on httbin.org, with both requests using ID or not :
http://www.httpbin.org/post?id=123&ware=were
http://www.httpbin.org/post
At least, we confirmed that the problem is not linked to the website I'm working on, but to the way fiddler is handling Json objects ...
I'm almost sure that If it works on httpbin.org, it will work anywere ...
Can you see any reason for fiddler to badly reading the Jsons ?
I tried activating session decoding :
oSession.utilDecodeResponse();
but it doesn't work, with it or without ... any ideas ?
Hi Oudin,
Line 2 of your code snippet needs top be changed to the following:
var json:JSON.JSONParseResult = JSON.JsonDecode(sInput);
In my testing the above line was required to convert the JsonDecode to a JSONParseResult. Doing this allowed viewing the JSONObject's properties.
The reason for this is described in Eric's comment on the How to Call JSON Methods inside Fiddler Script which is that Fiddler's JSON Object's are not optimized for Fiddler Script.
Please give this a try and let me know the results. Thank you and I look forward to your reply.
Regards,
Eric R | Senior Technical Support Engineer
Progress Telerik
Hello Eric,
I'm getting a newbie error :
Typename expected
I'm guessing a missing include/import at the beginning of the script ... Bit I don't know the Jscript modules very well ...
As a reminder, this line works well ASIS :
var oJSON = Fiddler.WebFormats.JSON.JsonDecode(sInput);
Hi Oudin,
My apologies, the required import is Fiddler.WebFormats. However, the issue with the provided code is the GetResponseBodyAsString method is not called. Add parentheses to get the string response body value. See the following code snippet for a more complete reference.
import Fiddler.WebFormats;
// Code Removed for Brevity //
static function OnBeforeResponse(oSession: Session) {
if (m_Hide304s && oSession.responseCode == 304) {
oSession["ui-hide"] = "true";
}
var sInput = oSession.GetResponseBodyAsString();
var json:JSON.JSONParseResult = Fiddler.WebFormats.JSON.JsonDecode(sInput);
var oObj = json.JSONObject;
FiddlerApplication.Log.LogString(oObj["url"]);
}
Please give this a try. Thank you for using the Fiddler Forums.
Regards,
Eric R | Senior Technical Support Engineer
Progress Telerik
Hello Eric.
Well met ! Fixing the missing parentheses fixed it, and everything is working fine now.
Thank you so much for the time spent with me !