Home > Software design >  how to sum up json keys value in array if by putting condition on key
how to sum up json keys value in array if by putting condition on key

Time:11-18

I have a json array like this below , but i want to aggregate this by condition if their userid , groupname , segment & exchange is matching then perform sum operation on all jsonkeys and give output as a single json row.

[{"userid":"CNAF01","groupname":"Abhay","segment":"CosCLFIN","accountcode":"66CN-063CC","symbol":"INX22","exchange":"SGXFO","expirydate":"24NOV2022",
"scripcode":"7536847","securitytype":"FUT","strikeprice":0,"opttype":"XX","bfqty":-107,"bfrate":18462.05,"bfamt":3950878.6999999997,"buyqty":21,"sellqty":59,"netqty":-38,
"cfqty":-145,"cfamt":5358027.699999999,"ltp":"18433.5","grossmtm":0,"brokerageamt":48,"netmtm":0,"currency":"USD","usdrate":81.33,"clientsharing":50,"broksharing":0,
"comsharing":50,"multiplier":2},

{"userid":"CNAF01","groupname":"Abhay","segment":"CosCLFIN","accountcode":"CW05","symbol":"NIFTY","exchange":"NSEFO",
"expirydate":"24NOV2022","scripcode":"61349","securitytype":"OPT","strikeprice":17800,"opttype":"CE","bfqty":4050,"bfrate":673.3,"bfamt":-2726865,"buyqty":1650,
"sellqty":300,"netqty":1350,"cfqty":5400,"cfamt":-3677257.5,"ltp":"657.0","grossmtm":0,"brokerageamt":1038.81126525,"netmtm":0,"currency":"INR","usdrate":1,"clientsharing":0,
"broksharing":0,"comsharing":100,"multiplier":1},

{"userid":"CNAF01","groupname":"Abhay","segment":"CosCLFIN","accountcode":"CW05","symbol":"NIFTY","exchange":"NSEFO",
"expirydate":"24NOV2022","scripcode":"61350","securitytype":"OPT","strikeprice":17800,"opttype":"PE","bfqty":-4050,"bfrate":14.7,"bfamt":59535,"buyqty":300,"sellqty":1650,
"netqty":-1350,"cfqty":-5400,"cfamt":78900,"ltp":"13.6","grossmtm":0,"brokerageamt":31.850901,"netmtm":0,"currency":"INR","usdrate":1,"clientsharing":0,"broksharing":0,
"comsharing":100,"multiplier":1},

Expected Result :

[{"userid":"CNAF01","groupname":"Abhay","segment":"CosCLFIN","accountcode":"66CN-063CC","symbol":"INX22","exchange":"SGXFO","expirydate":"24NOV2022",
"scripcode":"7536847","securitytype":"FUT","strikeprice":0,"opttype":"XX","bfqty":-107,"bfrate":18462.05,"bfamt":3950878.6999999997,"buyqty":21,"sellqty":59,"netqty":-38,
"cfqty":-145,"cfamt":5358027.699999999,"ltp":"18433.5","grossmtm":0,"brokerageamt":48,"netmtm":0,"currency":"USD","usdrate":81.33,"clientsharing":50,"broksharing":0,
"comsharing":50,"multiplier":2},

{"userid":"CNAF01","groupname":"Abhay","segment":"CosCLFIN","accountcode":"CW05","symbol":"NIFTY","exchange":"NSEFO",
"expirydate":"24NOV2022","scripcode":"61349","securitytype":"OPT","strikeprice":17800,"opttype":"CE","bfqty":4050,"bfrate":673.3,"bfamt":-2726865,"buyqty":1650,
"sellqty":300,"netqty":1350,"cfqty":0,"cfamt":-3598357.5,"ltp":"670.6","grossmtm":0,"brokerageamt":1038.81126525,"netmtm":0,"currency":"INR","usdrate":1,"clientsharing":0,
"broksharing":0,"comsharing":100,"multiplier":1},

As , you can easily se change in "cfamt" and "ltp" , additon performed on that.

I have tried using linkedhashmap :

   Map<String, Integer> map = new LinkedHashMap<>();

                            for (int j = 0; j < jsArraynetposition.length(); j  ) {
                                JSONObject list = jsArraynetposition.getJSONObject(j);


                                String key = list.getString("userid")   list.getString("groupname")   list.getString("segment");
   
                                if (!map.containsKey(key)) {
                               
                 map.put(key, list.getInt("cfamt"));

                                } else {
                                 
              int val = map.get(key)   list.getInt("cfamt");
              map.put(key , val);
                                }
                                    System.out.println("map "   map);

output got correct but keys and structure changed :

2022-11-17 10:16:17.199 21781-21828 System.out              com.example.sharedemo                I  map {CNAF01AbhayCosCLFIN=-3936830, CNCB08ChotuBansalCosPROFIN=-65762393, CNFAB01ArunCosPROFIN=132064, CNFD01CosmicCosPROFIN=52359450, CNFD03ManavCosPROFIN=937236, CNFNC01Nilesh_jiCosPROFIN=-290483, CNFRS01RichelleCosPROFIN=87907, CNFSG01SatishCosPROFIN=2824676, CNGBM01BishanMontyCosPROGOG=-2015145, CNGCB04ChotuBansalCosPROGOG=-7306932, CNGCB05ChotuBansalCosPROGOG=-18177730, CNGCB09ChotuBansalCosPROGOG=-924494, CNGCB10ChotuBansalCosPROGOG=-922274, CNGCB11ChotuBansalCosPROGOG=-862604, CNGCB12ChotuBansalCosPROGOG=-106035, CNGHG01HarshCosPROGOG=-1370947, CNGRV01RaviGuptaCosPROGOG=174712}

CodePudding user response:

Use library Josson to do the job.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "[{\"userid\":\"CNAF01\",\"groupname\":\"Abhay\",\"segment\":\"CosCLFIN\",\"accountcode\":\"66CN-063CC\",\"symbol\":\"INX22\",\"exchange\":\"SGXFO\",\"expirydate\":\"24NOV2022\","  
    "\"scripcode\":\"7536847\",\"securitytype\":\"FUT\",\"strikeprice\":0,\"opttype\":\"XX\",\"bfqty\":-107,\"bfrate\":18462.05,\"bfamt\":3950878.6999999997,\"buyqty\":21,\"sellqty\":59,\"netqty\":-38,"  
    "\"cfqty\":-145,\"cfamt\":5358027.699999999,\"ltp\":\"18433.5\",\"grossmtm\":0,\"brokerageamt\":48,\"netmtm\":0,\"currency\":\"USD\",\"usdrate\":81.33,\"clientsharing\":50,\"broksharing\":0,"  
    "\"comsharing\":50,\"multiplier\":2},"  
    "{\"userid\":\"CNAF01\",\"groupname\":\"Abhay\",\"segment\":\"CosCLFIN\",\"accountcode\":\"CW05\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\","  
    "\"expirydate\":\"24NOV2022\",\"scripcode\":\"61349\",\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"CE\",\"bfqty\":4050,\"bfrate\":673.3,\"bfamt\":-2726865,\"buyqty\":1650,"  
    "\"sellqty\":300,\"netqty\":1350,\"cfqty\":5400,\"cfamt\":-3677257.5,\"ltp\":\"657.0\",\"grossmtm\":0,\"brokerageamt\":1038.81126525,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,"  
    "\"broksharing\":0,\"comsharing\":100,\"multiplier\":1},"  
    "{\"userid\":\"CNAF01\",\"groupname\":\"Abhay\",\"segment\":\"CosCLFIN\",\"accountcode\":\"CW05\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\","  
    "\"expirydate\":\"24NOV2022\",\"scripcode\":\"61350\",\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"PE\",\"bfqty\":-4050,\"bfrate\":14.7,\"bfamt\":59535,\"buyqty\":300,\"sellqty\":1650,"  
    "\"netqty\":-1350,\"cfqty\":-5400,\"cfamt\":78900,\"ltp\":\"13.6\",\"grossmtm\":0,\"brokerageamt\":31.850901,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,"  
    "\"comsharing\":100,\"multiplier\":1}]");

Transformation

JsonNode node = josson.getNode(
    "group(map(userid,groupname,segment,accountcode,symbol,exchange,expirydate,"  
    "          currency,usdrate,clientsharing,broksharing,comsharing,multiplier))"  
    ".map(key.userid,"  
    "     key.groupname,"  
    "     key.segment,"  
    "     key.accountcode,"  
    "     key.symbol,"  
    "     key.exchange,"  
    "     key.expirydate,"  
    "     key.currency,"  
    "     key.usdrate,"  
    "     key.clientsharing,"  
    "     key.broksharing,"  
    "     key.comsharing,"  
    "     key.multiplier,"  
    "     elements.bfqty.sum(),"  
    "     elements.bfrate.sum(),"  
    "     elements.bfamt.sum(),"  
    "     elements.buyqty.sum(),"  
    "     elements.sellqty.sum(),"  
    "     elements.netqty.sum(),"  
    "     elements.cfqty.sum(),"  
    "     elements.cfamt.sum(),"  
    "     elements.ltp.sum(),"  
    "     elements.grossmtm.sum(),"  
    "     elements.brokerageamt.sum(),"  
    "     elements.netmtm.sum())");
System.out.println(node.toPrettyString());

Output

[ {
  "userid" : "CNAF01",
  "groupname" : "Abhay",
  "segment" : "CosCLFIN",
  "accountcode" : "66CN-063CC",
  "symbol" : "INX22",
  "exchange" : "SGXFO",
  "expirydate" : "24NOV2022",
  "currency" : "USD",
  "usdrate" : 81.33,
  "clientsharing" : 50,
  "broksharing" : 0,
  "comsharing" : 50,
  "multiplier" : 2,
  "bfqty" : -107.0,
  "bfrate" : 18462.05,
  "bfamt" : 3950878.6999999997,
  "buyqty" : 21.0,
  "sellqty" : 59.0,
  "netqty" : -38.0,
  "cfqty" : -145.0,
  "cfamt" : 5358027.699999999,
  "ltp" : 18433.5,
  "grossmtm" : 0.0,
  "brokerageamt" : 48.0,
  "netmtm" : 0.0
}, {
  "userid" : "CNAF01",
  "groupname" : "Abhay",
  "segment" : "CosCLFIN",
  "accountcode" : "CW05",
  "symbol" : "NIFTY",
  "exchange" : "NSEFO",
  "expirydate" : "24NOV2022",
  "currency" : "INR",
  "usdrate" : 1,
  "clientsharing" : 0,
  "broksharing" : 0,
  "comsharing" : 100,
  "multiplier" : 1,
  "bfqty" : 0.0,
  "bfrate" : 688.0,
  "bfamt" : -2667330.0,
  "buyqty" : 1950.0,
  "sellqty" : 1950.0,
  "netqty" : 0.0,
  "cfqty" : 0.0,
  "cfamt" : -3598357.5,
  "ltp" : 670.6,
  "grossmtm" : 0.0,
  "brokerageamt" : 1070.66216625,
  "netmtm" : 0.0
} ]
  • Related