Что я сделал, так это перебирал объекты в строке, а затем перебирал атрибуты каждого объекта. Надеюсь, это поможет вам решить вашу проблему. Кроме того, в вашей исходной строке вам не хватает открытых скобок и закрывающих скобок, поэтому я добавил их.
String jsonWithOutFormat = "param1, param2, param3{npam1, npam2, npam3{nipam1, nipam2}}";
jsonWithOutFormat = jsonWithOutFormat.replaceAll(" ", "");
String json = "";
String[] objectsInString = jsonWithOutFormat.split("[{]");
List<String> nestedObjects = new ArrayList<>();
json += "{";
for (int i = 0; i < objectsInString.length; i++) {
String[] objectAttributes = objectsInString[i].split("[,]");
if(i==0)
nestedObjects.add(objectAttributes[objectAttributes.length-1] + ".");
else
nestedObjects.add(nestedObjects.get(i-1)+objectAttributes[objectAttributes.length-1] + ".");
for (int j = 0; j < objectAttributes.length; j++) {
if(!(j == objectAttributes.length-1)) {
if(i != 0)
json+= objectAttributes[j] + ": " + nestedObjects.get(i-1) + objectAttributes[j] + ", ";
else
json+= objectAttributes[j] + "\"" + ": " + "\"" + objectAttributes[j] + "\"" + ", ";
}
else {
if(!(i == objectsInString.length-1))
json+= objectAttributes[j] + ": {";
else {
json+= objectAttributes[j].replaceAll("}", "") + ": " + nestedObjects.get(i-1) + objectAttributes[j];
}
}
}
}
json += "}";
System.out.print("\n" + json);
}