0

I would like to ask if it is possible to group the object by another object inside its common array.

Here's the JSON response, I need to group the list of item by program id. I'm trying to put it on the HashMap but it didn't work well.

{
"id": "",
"ordered_by": 64,
"order_details": [
    {
        "resource": "Product",
        "required_prescription": false,
        "item": {
            "id": 6,
            "name": "Synergistic Copper Gloves",
            "code": "51537661-C",
            "enabled": true,
            "generic_name": "Mediocre Steel Wallet",
            "price_cents": 200000
        },
        "program": {
            "id": 12,
            "name": "Synergistic Wooden Shoes",
            "provider": "Synergistic Rubber Coat",
            "discount_type": "fixed"
        }
    },
    {
        "resource": "Product",
        "required_prescription": true,
        "item": {
            "id": 7,
            "name": "Rustic Leather Table",
            "code": "74283131-P",
            "enabled": true,
            "generic_name": "Incredible Bronze Clock",
            "price_cents": 8994
        },
        "program": {
            "id": 12,
            "name": "Synergistic Wooden Shoes",
            "provider": "Synergistic Rubber Coat",
            "discount_type": "fixed"
        }
    },
    {
        "resource": "Product",
        "required_prescription": false,
        "item": {
            "id": 116,
            "name": "Ergonomic Marble Hat",
            "code": "98845056-A",
            "enabled": true,
            "generic_name": "Incredible Granite Lamp",
            "price_cents": 8267
        },
        "program": {
            "id": 10,
            "name": "Durable Rubber Bag",
            "provider": "Aerodynamic Steel Chair",
            "discount_type": "fixed"
        }
    }
]}

This should be the expected object after grouping. The item was grouped by program id 12 & 10.

[
  {
    "id": 12,
    "name": "Synergistic Wooden Shoes",
    "provider": "Synergistic Rubber Coat",
    "discount_type": "fixed",
    "item": [
      {
        "id": 6,
        "name": "Synergistic Copper Gloves",
        "code": "51537661-C",
        "enabled": true,
        "generic_name": "Mediocre Steel Wallet",
        "price_cents": 200000
      },
      {
        "id": 7,
        "name": "Rustic Leather Table",
        "code": "74283131-P",
        "enabled": true,
        "generic_name": "Incredible Bronze Clock",
        "price_cents": 8994
      }
    ]
  },
  {
    "id": 10,
    "name": "Durable Rubber Bag",
    "provider": "Aerodynamic Steel Chair",
    "discount_type": "fixed",
    "item": [
      {
        "id": 116,
        "name": "Ergonomic Marble Hat",
        "code": "98845056-A",
        "enabled": true,
        "generic_name": "Incredible Granite Lamp",
        "price_cents": 8267
      }
    ]
  }
]

All comments would be highly appreciated. Thanks in advance!

2 Answers 2

2

I have taken your source json and tried to convert it as per your specification and this is the solution which is working, pass your source JSON as string and you will get the desired output

private String parseJson(String source) {
    JSONArray result = new JSONArray();
    List<Integer> ids = new ArrayList<>();
    HashMap<Integer,JSONObject> programs = new HashMap<>();
    try {
        JSONObject jSource = new JSONObject(source);
        JSONArray orderDetails = jSource.getJSONArray("order_details");
        if (orderDetails.length() > 0) {
            for (int i = 0; i < orderDetails.length(); i++) {
                JSONObject jsonObject = orderDetails.getJSONObject(i);
                JSONObject item = jsonObject.getJSONObject("item");
                JSONObject program = jsonObject.getJSONObject("program");
                int programId = jsonObject.getJSONObject("program").getInt("id");
                if (!ids.contains(programId)) {
                    ids.add(programId);
                    program.put("item",new JSONArray().put(item));
                    programs.put(programId,program);
                }else{
                    program.put("item",programs.get(programId).getJSONArray("item").put(item));
                }
            }

            for(int k :programs.keySet()){
                result.put(programs.get(k));
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result.toString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

if it works for you then please accept this as your answer
It works! Thank you very much @trinadh! You're a lifesaver, is it also possible to convert the string to object? TIA
0

JSON library Josson can transform the JSON by a simple expression.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "\"id\": \"\"," +
    "\"ordered_by\": 64," +
    "\"order_details\": [" +
    "    {" +
    "        \"resource\": \"Product\"," +
    "        \"required_prescription\": false," +
    "        \"item\": {" +
    "            \"id\": 6," +
    "            \"name\": \"Synergistic Copper Gloves\"," +
    "            \"code\": \"51537661-C\"," +
    "            \"enabled\": true," +
    "            \"generic_name\": \"Mediocre Steel Wallet\"," +
    "            \"price_cents\": 200000" +
    "        }," +
    "        \"program\": {" +
    "            \"id\": 12," +
    "            \"name\": \"Synergistic Wooden Shoes\"," +
    "            \"provider\": \"Synergistic Rubber Coat\"," +
    "            \"discount_type\": \"fixed\"" +
    "        }" +
    "    }," +
    "    {" +
    "        \"resource\": \"Product\"," +
    "        \"required_prescription\": true," +
    "        \"item\": {" +
    "            \"id\": 7," +
    "            \"name\": \"Rustic Leather Table\"," +
    "            \"code\": \"74283131-P\"," +
    "            \"enabled\": true," +
    "            \"generic_name\": \"Incredible Bronze Clock\"," +
    "            \"price_cents\": 8994" +
    "        }," +
    "        \"program\": {" +
    "            \"id\": 12," +
    "            \"name\": \"Synergistic Wooden Shoes\"," +
    "            \"provider\": \"Synergistic Rubber Coat\"," +
    "            \"discount_type\": \"fixed\"" +
    "        }" +
    "    }," +
    "    {" +
    "        \"resource\": \"Product\"," +
    "        \"required_prescription\": false," +
    "        \"item\": {" +
    "            \"id\": 116," +
    "            \"name\": \"Ergonomic Marble Hat\"," +
    "            \"code\": \"98845056-A\"," +
    "            \"enabled\": true," +
    "            \"generic_name\": \"Incredible Granite Lamp\"," +
    "            \"price_cents\": 8267" +
    "        }," +
    "        \"program\": {" +
    "            \"id\": 10," +
    "            \"name\": \"Durable Rubber Bag\"," +
    "            \"provider\": \"Aerodynamic Steel Chair\"," +
    "            \"discount_type\": \"fixed\"" +
    "        }" +
    "    }" +
    "]}");

Transformation

JsonNode node = josson.getNode(
    "order_details.group(program, item).field(program:, **:program)");
System.out.println(node.toPrettyString());

Output

[ {
  "item" : [ {
    "id" : 6,
    "name" : "Synergistic Copper Gloves",
    "code" : "51537661-C",
    "enabled" : true,
    "generic_name" : "Mediocre Steel Wallet",
    "price_cents" : 200000
  }, {
    "id" : 7,
    "name" : "Rustic Leather Table",
    "code" : "74283131-P",
    "enabled" : true,
    "generic_name" : "Incredible Bronze Clock",
    "price_cents" : 8994
  } ],
  "id" : 12,
  "name" : "Synergistic Wooden Shoes",
  "provider" : "Synergistic Rubber Coat",
  "discount_type" : "fixed"
}, {
  "item" : [ {
    "id" : 116,
    "name" : "Ergonomic Marble Hat",
    "code" : "98845056-A",
    "enabled" : true,
    "generic_name" : "Incredible Granite Lamp",
    "price_cents" : 8267
  } ],
  "id" : 10,
  "name" : "Durable Rubber Bag",
  "provider" : "Aerodynamic Steel Chair",
  "discount_type" : "fixed"
} ]

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.