shek shek - 2 months ago 4
Javascript Question

Not able to replace all occurence of a json key correctly

Hi I have a nested json object (showing output of jsp page from console.log):

0:Object
total_exec_qty_buy
:
"41377"
total_exec_val_buy
:
"2761601.00"
total_num_ords_buy
:
"726"
total_ord_qty_buy
:
"52854"
total_wt_arr_last_slp_buy
:
"-0.0779"
total_wt_arr_slp_buy
:
"-2.0691"
total_wt_buy
:
"0.31"
total_wt_ivwap_slp_buy
:
"-0.3358"
__proto__
:
Object
1:Object
total_exec_qty_sell
:
"95983"
total_exec_val_sell
:
"6172561.00"
total_num_ords_sell
:
"956"
total_ord_qty_sell
:
"123090"
total_wt_arr_last_slp_sell
:
"-1.3680"
total_wt_arr_slp_sell
:
"-2.6036"
total_wt_ivwap_slp_sell
:
"-0.2495"
total_wt_sell
:
"0.69"
__proto__
:
Object
2:Object
total_exec_qty
:
"137360"
total_notional
:
"8934153.27"
total_num_ords
:
"1682"
total_ord_qty
:
"175944"
total_wt_arr_last_slp
:
"-0.9692"
total_wt_arr_slp
:
"-2.4384"
total_wt_ivwap_slp
:
"-0.2761"


I want to rename all occurrences of
total_exec_qty_buy
&
total_exec_qty_sell
to
total_exec_qty
;
total_num_ords_buy
&
total_num_ords_sell
to
total_num_ords


I'm doing it in the following way:

newArray = JSON.stringify(newArray).split('"total_exec_qty_buy":').join('"total_exec_qty":');


When I do just one statement(above one) then the output is:

[{"total_wt_arr_slp_buy":"-2.0039","total_exec_qty":"44913","total_wt_buy":"0.30","total_wt_arr_last_slp_buy":"-0.0632","total_wt_ivwap_slp_buy":"-0.2404","total_exec_val_buy":"3007250.00","total_ord_qty_buy":"56799","total_num_ords_buy":"834"},{"total_wt_arr_last_slp_sell":"-1.2908","total_ord_qty_sell":"137158","total_exec_qty_sell":"108494","total_wt_ivwap_slp_sell":"-0.1699","total_wt_sell":"0.70","total_exec_val_sell":"6949689.00","total_num_ords_sell":"1066","total_wt_arr_slp_sell":"-2.5335"},{"total_wt_arr_last_slp":"-0.9201","total_exec_qty":"153407","total_wt_ivwap_slp":"-0.1912","total_wt_arr_slp":"-2.3735","total_ord_qty":"193957","total_num_ords":"1900","total_notional":"9956921.29"}]


But when I do this:

newArray = JSON.stringify(newArray).split('"total_exec_qty_buy":').join('"total_exec_qty":');
newArray = JSON.stringify(newArray).split('"total_exec_qty_sell":').join('"total_exec_qty":');
newArray = JSON.stringify(newArray).split('"total_num_ords_buy":').join('"total_num_ords":');
newArray = JSON.stringify(newArray).split('"total_num_ords_sell":').join('"total_num_ords":');


then I get this output:

"\"\\\"[{\\\\\\\"total_wt_arr_slp_buy\\\\\\\":\\\\\\\"-1.9322\\\\\\\",\\\\\\\"total_exec_qty\\\\\\\":\\\\\\\"49654\\\\\\\",\\\\\\\"total_wt_buy\\\\\\\":\\\\\\\"0.31\\\\\\\",\\\\\\\"total_wt_arr_last_slp_buy\\\\\\\":\\\\\\\"-0.1924\\\\\\\",\\\\\\\"total_wt_ivwap_slp_buy\\\\\\\":\\\\\\\"-0.1103\\\\\\\",\\\\\\\"total_exec_val_buy\\\\\\\":\\\\\\\"3224372.00\\\\\\\",\\\\\\\"total_ord_qty_buy\\\\\\\":\\\\\\\"61688\\\\\\\",\\\\\\\"total_num_ords_buy\\\\\\\":\\\\\\\"859\\\\\\\"},{\\\\\\\"total_wt_arr_last_slp_sell\\\\\\\":\\\\\\\"-1.2509\\\\\\\",\\\\\\\"total_ord_qty_sell\\\\\\\":\\\\\\\"139654\\\\\\\",\\\\\\\"total_exec_qty_sell\\\\\\\":\\\\\\\"111012\\\\\\\",\\\\\\\"total_wt_ivwap_slp_sell\\\\\\\":\\\\\\\"-0.1833\\\\\\\",\\\\\\\"total_wt_sell\\\\\\\":\\\\\\\"0.69\\\\\\\",\\\\\\\"total_exec_val_sell\\\\\\\":\\\\\\\"7129344.00\\\\\\\",\\\\\\\"total_num_ords_sell\\\\\\\":\\\\\\\"1099\\\\\\\",\\\\\\\"total_wt_arr_slp_sell\\\\\\\":\\\\\\\"-2.4978\\\\\\\"},{\\\\\\\"total_wt_arr_last_slp\\\\\\\":\\\\\\\"-0.9213\\\\\\\",\\\\\\\"total_exec_qty\\\\\\\":\\\\\\\"160666\\\\\\\",\\\\\\\"total_wt_ivwap_slp\\\\\\\":\\\\\\\"-0.1606\\\\\\\",\\\\\\\"total_wt_arr_slp\\\\\\\":\\\\\\\"-2.3216\\\\\\\",\\\\\\\"total_ord_qty\\\\\\\":\\\\\\\"201342\\\\\\\",\\\\\\\"total_num_ords\\\\\\\":\\\\\\\"1958\\\\\\\",\\\\\\\"total_notional\\\\\\\":\\\\\\\"10353698.63\\\\\\\"}]\\\"\""


Why is this happening??

Answer

This is happening because you keep calling JSON.stringify().

Stringify the json object once and then use str.replace() instead:

var jsonStr = JSON.stringify(jsonObj);
jsonStr = jsonStr.replace("total_exec_qty_buy", "total_exec_qty");    
jsonStr = jsonStr.replace("total_exec_qty_sell", "total_exec_qty"); 
jsonStr = jsonStr.replace("total_num_ords_buy", "total_num_ords"); 
jsonStr = jsonStr.replace("total_num_ords_sell", "total_num_ords"); 
Comments