0

im getting my data as whole json,but i expect it to be grouped based on a common element among the result itself

Data from query

firststring secondstring thirdstring
My Account MyAccount Menu1
My Cart MyFavourites Menu1
My Status Status Menu1
Orders orders Menu2
Damage Claim DamageClaim Menu2

my bean class

    public class CommonThreeString implements Serializable{

        private String firstString;
        private String secondString;
        private String thirdString;
        
        public String getFirstString() {
            return firstString;
        }
        public void setFirstString(String firstString) {
            this.firstString = firstString;
        }
        public String getSecondString() {
            return secondString;
        }
        public void setSecondString(String secondString) {
            this.secondString = secondString;
        }
        public String getThirdString() {
            return thirdString;
        }
        public void setThirdString(String thirdString) {
            this.thirdString = thirdString;
        }
    }

My controller

        @PostMapping("getMenuList")
    public String getMenuList() throws ServiceException {
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String json = null;
        try {
            json = ow.writeValueAsString(iService.getMenuList());
        } catch (JsonProcessingException e) {
        }

        return json;
    }

service implementation

        public List<CommonThreeString> getMenuList() throws ServiceException {
    
        List<CommonTwoLongThreeString> menuList = null;
        try {
            menuList =  Impl.getMenuListByPortalId(Long.valueOf("input"));
            //query works done here//
        } catch (ServiceException e) {
        }
        return menuList;
    }

here im getting output as

 [   
      {
        "firstString": "My Account",
        "secondString": "MyAccount",
        "thirdString": "Menu1"
      },
      {
        "firstString": "My Cart",
        "secondString": "MyFavourites",
        "thirdString": "Menu1"
      },
      {
        "firstString": "My Status",
        "secondString": "Status",
        "thirdString": "Menu1"
      },
      {
        "firstString": "orders",
        "secondString": "orders",
        "thirdString": "Menu2"
      },
      {
        "firstString": "Damage Claim",
        "secondString": "DamageClaim",
        "thirdString": "Menu2"
      }
    ]

The output im expecting

    Menu1
    [   
      {
        "firstString": "My Account",
        "secondString": "MyAccount",
        --"thirdString": "Menu1"
      },
      {
        "firstString": "My Cart",
        "secondString": "MyFavourites",
        --"thirdString": "Menu1"
      },
      {
        "firstString": "My Status",
        "secondString": "Status",
        --"thirdString": "Menu1"
      }
      ]
      Menu2
      [
      {
        "firstString": "orders",
        "secondString": "orders",
        --"thirdString": "Menu2"
      },
      {
        "firstString": "Damage Claim",
        "secondString": "DamageClaim",
        --"thirdString": "Menu2"
      }
    ]

the third String need to be the grouping factor

im want to group properly like the above thanks in advance

1 Answer 1

0
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.google.gson.*;

public class TestJson {
    public static void main(String[] args) {
        String jsonString = null;
        try {
            jsonString = "[\n"
                    + "    {\n"
                    + "        \"firstString\": \"My Account\",\n"
                    + "        \"secondString\": \"MyAccount\",\n"
                    + "        \"thirdString\": \"Menu1\"\n"
                    + "    },\n"
                    + "    {\n"
                    + "        \"firstString\": \"My Cart\",\n"
                    + "        \"secondString\": \"MyFavourites\",\n"
                    + "        \"thirdString\": \"Menu1\"\n"
                    + "    },\n"
                    + "    {\n"
                    + "        \"firstString\": \"My Status\",\n"
                    + "        \"secondString\": \"Status\",\n"
                    + "        \"thirdString\": \"Menu1\"\n"
                    + "    },\n"
                    + "    {\n"
                    + "        \"firstString\": \"orders\",\n"
                    + "        \"secondString\": \"orders\",\n"
                    + "        \"thirdString\": \"Menu2\"\n"
                    + "    },\n"
                    + "    {\n"
                    + "        \"firstString\": \"Damage Claim\",\n"
                    + "        \"secondString\": \"DamageClaim\",\n"
                    + "        \"thirdString\": \"Menu2\"\n"
                    + "    }\n"
                    + "]";
            
            ObjectMapper mapper = new ObjectMapper();


            com.google.gson.JsonArray dataArray = new JsonParser().parse(jsonString).getAsJsonArray();

            HashMap<String, List<CommonThreeString>> groupByFieldMap = new HashMap<String, List<CommonThreeString>>();

            for (JsonElement element : dataArray) {
                String groupField = element.getAsJsonObject().get("thirdString").getAsString();
                String firstStr = element.getAsJsonObject().get("firstString").getAsString();
                String secondStr = element.getAsJsonObject().get("secondString").getAsString();
                String thirdStr = element.getAsJsonObject().get("thirdString").getAsString();
                if (groupByFieldMap.containsKey(groupField)) {
                    groupByFieldMap.get(groupField).add(new CommonThreeString(firstStr, secondStr, thirdStr));
                } else {
                    ArrayList<CommonThreeString> emptyList = new ArrayList<CommonThreeString>();
                    emptyList.add(new CommonThreeString(firstStr, secondStr, thirdStr));
                    groupByFieldMap.put(groupField, emptyList);
                }
            }

            StringWriter result = new StringWriter();
            mapper.writeValue(result, groupByFieldMap);
            System.out.println(result.toString());
            
            
        } catch (Exception e) {
            System.out.println("Exception "+e.getStackTrace());
        }

    }

}

Ref Here : how to do it

Grouping Json response with keys in java - android studio

Seems to be a duplicate ticket

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.