I am dynamically producing a JSON as shown below with a Map Data

Currently this is producing the following JSON

{
"item": {
"T1": [
{
"name": "Chips & Chocolates",
"T2": [
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts",
"leaf": [
{
"crust_name": "crust"
}
]
}
]
},
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Nimbu filled",
"leaf": [
{
"crust_name": "crust"
}
]
}
]
}
]
}
]
}
}

But i need to create the following JSON

{
"item": {
"T1": [
{
"name": "Chips & Chocolates",
"T2": [
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts",
"leaf": [
{
"crust_name": "crust"
}
]
},
{
"name": "Chips & Chocolates***Bummy Chips***Nimbu filled",
"leaf": [
{
"crust_name": "crust"
}
]
}
]
}
]
}
]
}
}
Every comma seperated value inside the Map autoincrements the T level

Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();
List<String> values = new LinkedList<String>();

values.add("Bummy Chips,Masala Roasted with peanuts(49)");
values.add("Bummy Chips,Nimbu filled(50)");
consilatedMapMap.put("Chips & Chocolates", values);

This is my whole program

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

private static JSONObject processString(String data, int level,String key) throws JSONException {
JSONObject json = new JSONObject();
JSONArray leafjsonarray = new JSONArray();
int index = data.indexOf(',');
String name = data;
String remainder = "";
if (index < 0) {
index = name.indexOf('(');
if (index > 0) {
name = data.substring(0, index);
}
} else {
name = data.substring(0, index);
remainder = data.substring(name.length() + 1);
}
String fullpath = key+"***"+name;
json.put("name", fullpath);

JSONArray a = new JSONArray();
if (remainder.length() > 0) {
a.put(processString(remainder, level + 1,fullpath));
json.put("T" + level, a);
}

else {


JSONObject leafjsonObj = new JSONObject();
leafjsonObj.put("crust_name", "crust");
leafjsonarray.put(leafjsonObj);
json.put("leaf", leafjsonarray);


}
return json;
}

private static JSONArray processList(List<String> list, int level,String key) throws JSONException {
JSONArray json = new JSONArray();
for (String data : list) {
json.put(processString(data, level,key));
}
return json;
}

private static JSONArray processMap(Map<String, List<String>> map, int level) throws JSONException {

JSONArray array =new JSONArray();
for (String key : map.keySet()) {
JSONObject json = new JSONObject();
json.put("name", key);
json.put("T" + level, processList(map.get(key), level + 1,key));
array.put(json);
}
return array;

}

public static void main(String args[]) {
Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();

List<String> values = new LinkedList<String>();
values.add("Bummy Chips,Masala Roasted with peanuts(49)");
values.add("Bummy Chips,Nimbu filled(50)");


consilatedMapMap.put("Chips & Chocolates", values);


try {
int level = 2;
JSONArray json = processMap(consilatedMapMap, level);
JSONObject jsonT1 = new JSONObject();
jsonT1.put("T1",json);
JSONObject sai = new JSONObject();
sai.put("item",jsonT1);

System.out.println(sai);

} catch(JSONException x) {
x.printStackTrace();
System.exit(-1);
}
}
}