[JSON]:Replacing the "value" of a specific "key", replaced same "value" in different "key" too

412 views
Skip to first unread message

sandeep goyal

unread,
Dec 14, 2017, 7:14:50 PM12/14/17
to Fiddler

Hi Eric,

I have a question regarding replacing the value in nested JSON body. i am using c# Fiddler script to modify the value of nested json . for e.x:

i have below nested JSON body

{
 "ab":
  {
    "sz":1,
     "rm":
        {
         "av":2,
         "de":1,
          "k":[1,
              [4,
              {"dn":"ab","only":"ok","pow":0,"pr":"https"
              }
              ]
          }}}]

i have used below c# script to modify the value of key "de" from 1 to 11 as

 var sbody = oSession.GetResponseBodyAsString();
  Fiddler.WebFormats.JSON.JSONParseResult oJSON;
 oJSON = (Fiddler.WebFormats.JSON.JSONParseResult)Fiddler.WebFormats.JSON.JsonDecode(sbody);
 var oObj =  oJSON.JSONObject as Hashtable;
 var val = ((Hashtable) oObj["ab"]);
 var val1 = ((Hashtable) val["rm"]);
 var oParams = val1["de"];
        var value1 = 11;
 sbody = sbody.Replace(oParams.ToString(), value1.ToString());
 oSession.utilSetResponseBody(sbody);

After this what i see in Fiddler is that value for key "de" is got replaced by 11.

but  in my whole JSON body wherever 1 is present, that also got replaced by 11. in above JSON body value of "sz" and "k" also got replaced. New json after modification is now:
{
 "ab":
  {
    "sz":11,
     "rm":
        {
         "av":2,
         "de":11,
          "k":[11,
              [4,
              {"dn":"ab","only":"ok","pow":0,"pr":"https"
              }
              ]
          }}}]

i just want key "de" to be replaced as 11.

Could you please help me, where i am doing wrong?

sandeep goyal

unread,
Dec 14, 2017, 8:01:38 PM12/14/17
to Fiddler
As far as I understand,  sbody.Replace(oParams.ToString(), value1.ToString()); is just replacing the value 1 wherever it find in the whole body.

Is there any way, I can replace the value of val1["de"]?

sandeep goyal

unread,
Dec 14, 2017, 9:06:02 PM12/14/17
to Fiddler
no action required. I figure it out. thanks.
Reply all
Reply to author
Forward
0 new messages