How do I include one JSON in another JSON?

My first JSON is:

  "alternatives":    [1,2,3,4,5],  

This will come from a java URL, here I am using PHP, in PHP I am calling a java URL.

My Second JSON is:


I am generating this using JQuery. How can I include the second JSON into the first JSON criterias?


You need to convert your first JSON string to object and add a new property to it.

If it is Java, you can use Google’s Gson library

Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject();
jsonObj.add("criterias", "Location");

If it is JavaScript,

var jObj = JSON.parse(jsonString);
jObj.criterias = "Location";  //jObj.NewItem = "NewValue";

Leave a Reply

Your email address will not be published. Required fields are marked *