3

In Fiddler, I have a response body (application/json) that I convert into an Object using eval() (if there is a better way please let me know) to perform some checks. Now I want to convert several parts (but not all parts) of the object back into a string and save them to seperate files. How would I do this in Jscript?

I have tried using the ToString() method but it only returns [object Object]. Any ideas?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Jowic
  • 31
  • 3

2 Answers2

3

I had the same question and I asked Fiddler Forum about it. From the answer, I cooked this FiddlerScript (JScript) to answer yours:

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]);

I hope this will help after half a year...

Ek1noX
  • 443
  • 6
  • 11
2

JScript.NET does not itself include a JSON serializer.

You can either use one from the appropriate .NET assembly, or you can use the JSON serializer baked into Fiddler; have a look at the Fiddler.WebFormats.JSON.JsonEncode(object) function.

EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • It doesn't seem to work. `Fiddler.WebFormats.JSON.JsonEncode(object)` returns an empty string when used on an object like `var oTest: Object = {"a":"b"};` – Jowic Sep 30 '13 at 13:44
  • What *exactly* did you pass in to the function? Generally, you'd want to pass the value of `oSesssion.GetResponseBodyAsString()` into `JSON.JsonDecode(string)` rather than having JScript.NET perform its own `eval()`. – EricLaw Oct 01 '13 at 13:28
  • Yes that is what I pass in. Doing this `var oTest = Fiddler.WebFormats.JSON.JsonDecode(oSession.GetResponseBodyAsString());` `var sTest = Fiddler.WebFormats.JSON.JsonEncode(oTest);` returns an empty string on a simple test page I set up. – Jowic Oct 01 '13 at 13:57
  • @EricLaw When I use the above code in my FiddlerScript, I get the following error: Type 'Fiddler.WebFormats.JSON.JsonDecode could not be found, is an assembly reference missing? How do I fix this? – Matthew Dean Oct 10 '13 at 18:38
  • Are you running the latest build of Fiddler? – EricLaw Oct 10 '13 at 19:57