You can iterate via for loop and in every iteration create new JSONObject and add it to a collection. Finally add the collection to the mergedObject. E.g.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
public class Foo {
public static void main(String[] args) throws JSONException {
JSONObject object1 = new JSONObject("{
" +
" "name": [
" +
" "Karbonn Smart A12 Star (Black & Silver)",
" +
" "Nokia 220 (Black)",
" +
" "Karbonn Smart A52 Plus (Black & Gold)",
" +
" "Karbonn Smart A12 Star (White & Gold)",
" +
" "Karbonn Smart A50s (Black)",
" +
" "Karbonn Smart A52 Plus (White & Silver)",
" +
" "Karbonn Titanium S2 Plus (White)",
" +
" "Karbonn Smart A11 Star (Black)",
" +
" "Karbonn Smart A11 Star (White)"
" +
" ]
" +
"}");
JSONObject object2 = new JSONObject("{
" +
" "price": [
" +
" "Rs. 3,699",
" +
" "Rs. 2,599",
" +
" "Rs. 2,499",
" +
" "Rs. 3,699",
" +
" "Rs. 2,699",
" +
" "Rs. 2,499",
" +
" "Rs. 4,999",
" +
" "Rs. 4,399",
" +
" "Rs. 4,499"
" +
" ]
" +
"}");
JSONArray nameArray = object1.getJSONArray("name");
JSONArray priceArray = object2.getJSONArray("price");
JSONObject mergedObject = new JSONObject("{}");
Collection<JSONObject> collection = new ArrayList<>();
for (int i = 0; i < nameArray.length(); i++) {
JSONObject obj = new JSONObject();
obj.put("name", nameArray.getString(i));
obj.put("price", priceArray.getString(i));
collection.add(obj);
}
mergedObject.put("mobile", collection);
System.out.println(mergedObject);
}
}
Ouput:
{"mobile":[{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (Black & Silver)"},{"price":"Rs. 2,599","name":"Nokia 220 (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (Black & Gold)"},{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (White & Gold)"},{"price":"Rs. 2,699","name":"Karbonn Smart A50s (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (White & Silver)"},{"price":"Rs. 4,999","name":"Karbonn Titanium S2 Plus (White)"},{"price":"Rs. 4,399","name":"Karbonn Smart A11 Star (Black)"},{"price":"Rs. 4,499","name":"Karbonn Smart A11 Star (White)"}]}