title: Java , put JsonObject into JsonArray, but JsonArray take the same value at the end of the for loop
body: <p>I am a novice in Java, on this code  I am trying to put JsonObect in a JsonArray , but  the things is the JsonArray only takes the last value and it is duplicate in it.</p>
  
  <p>You do not need to understand all the code just the interesting part </p>
  
  <p>I have put  the part Of the code which is interesting  in the field //******here******// code   //******end here******// and I have also put the json file if you want to test </p>
  
  <p>For my part ,I have tried to put my values in List of JsonObject then put it in the JsonArray,but I have the same result.</p>
  
  <p>I have also tried to use a Hash map with indices but it still do not working  </p>
  
  <p>If you print the JsonObject variable "Alldate"  you can see that it has  different values but When I put them in the JsonArray Array "ArrayBuckets", you will see That the  JsonArray has the same value which is the last values.</p>
  
  <p>I do not understand because my jsonObject and Array are in the same loop.</p>
  
  <p>Thanks .</p>
  
  <p>Here is the code :</p>
  
  <pre><code>package jsonbi.bi;
  
  import java.io.FileReader;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import org.codehaus.jackson.JsonFactory;
  import org.codehaus.jackson.JsonNode;
  import org.codehaus.jackson.map.ObjectMapper;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.json.simple.parser.JSONParser;
  
  import com.google.gson.Gson;
  
  public class JsonBi9 {
  
      JSONParser parser = new JSONParser();
      JSONObject json;
      Object obj;
      Map.Entry&lt;String, JsonNode&gt; field;
      HashMap newmap = new HashMap();
      JsonFactory factory = new JsonFactory();
      private ObjectMapper mapper = new ObjectMapper();
  
      Gson gson = new Gson();
  
      JSONArray jsonArray = new JSONArray();
      JSONObject Alldate = new JSONObject();
  
      JSONObject BucketsObject = new JSONObject();
      JSONObject Groupe = new JSONObject();
      JSONObject Aggregations = new JSONObject();
      JSONObject TheJson = new JSONObject();
  
      JSONArray ArrayBuckets = new JSONArray();
  
       Hashtable ht = new Hashtable();
  
  
  
       int j =0;
  
      public JsonBi9() {
  
          try {
  
              // here I am readind the json file
              obj = parser.parse(new FileReader("myjson2.json"));
  
              ObjectMapper mapper = new ObjectMapper(factory);
  
              JsonNode rootNode = mapper.readTree(obj.toString());
  
              Iterator&lt;Map.Entry&lt;String, JsonNode&gt;&gt; fieldsIterator = rootNode.getFields();
  
              JSONObject jsonObject = new JSONObject(obj.toString());
  
  
              // here I am reading the fields in the json fields
              while (fieldsIterator.hasNext()) {
  
                  field = fieldsIterator.next();
  
                  newmap.put(field.getKey(), field.getValue());
  
                  if (field.getKey().equals("aggregations")) {
  
                      System.out.println("hugo");
  
                      JSONObject aggregations = jsonObject.getJSONObject("aggregations");
  
                      JSONObject groupe = aggregations.getJSONObject("groupe");
  
                      JSONArray buckets = groupe.getJSONArray("buckets");
  
  
  
      for (int i11a = 0; i11a &lt; buckets.length(); i11a++) {
  
  
  
      JSONObject obj1bucket = buckets.getJSONObject(i11a);
  
  
                          JSONObject date11 = obj1bucket.getJSONObject("date");
  
                          JSONArray obj11bucket = date11.getJSONArray("buckets");
  
  
  
                          List&lt;String&gt; listSubbucket = new ArrayList&lt;String&gt;();
  
                          for (int i11 = 0; i11 &lt; obj11bucket.length(); i11++) {
  
                              listSubbucket.add(obj11bucket.getJSONObject(i11).toString());
  
                              // System.out.println(obj11bucket.getJSONObject(i11).toString());
  
                          }
  
                          jsonArray = new JSONArray();
  
                          for (int i11 = 0; i11 &lt; listSubbucket.size(); i11++) {
  
                              JSONObject SubBucket = new JSONObject();
  
                              SubBucket.put("buckets", listSubbucket.get(i11));
  
  
                              getdocument(obj1bucket, Alldate);
  
  
  
  //******************************here ********************************//
  
                              // System.out.println(SubBucket);
                              Alldate.put("date", SubBucket);
  
  
                              ArrayBuckets.put(Alldate);
  
                          //*µµ if you want to print the jsonObject and array
  
                          //  System.out.println(Alldate);
                              // System.out.println(ArrayBuckets);*
  
  //*********************************end here ******************************//
  
  
  
                          } // end first  for loop
  
                      } //end second for loop
  
                      Enumeration e = ht.elements();
  
                      while(e.hasMoreElements())
                        System.out.println(e.nextElement());
  
  
  
                      getdocument(groupe, Groupe);
  
                      Groupe.put("buckets", ArrayBuckets);
  
                      Aggregations.put("groupe", Groupe);
                      // System.out.println(Aggregations);
  
                  } // fin du if
  
                  else {
  
                      // System.out.println("Key: " + field.getKey() + "\tValue:"
                      // + field.getValue());
  
                      TheJson.put(field.getKey(), field.getValue());
  
                  } // fin du else
  
              } // fin du while
  
              // I put all the documents
              TheJson.put("aggregations", Aggregations);
  
              System.out.println(TheJson);
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
      public void getdocument(JSONObject obj1bucket, JSONObject Alldate) {
  
          // get the json documents in the subbuckets
          int i = 0;
          // JSONObject buckectinfos1 = new JSONObject();
          for (Iterator key = obj1bucket.keys(); key.hasNext();) {
  
              Object jsonkeys;
              try {
                  jsonkeys = obj1bucket.get((String) key.next());
  
                  Object jsonvalues = obj1bucket.names().getString(i);
  
                  String jsonInString = gson.toJson(jsonkeys);
  
                  if (!jsonkeys.getClass().getName().equals("org.json.JSONObject")
                          &amp;&amp; !jsonkeys.getClass().getName().equals("org.json.JSONArray")) {
  
  
                  } // fin du if
                  i++;
  
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          } // fin du for
  
      }
  
  }
  </code></pre>
  
  <p>Here is the values in the JsonObject "alldate"  that I want to put  In the JsonArray "ArrayBuckets":</p>
  
  <pre><code>{"date":{"buckets":"{\"key_as_string\":\"2017-05-03T00:00:00.000Z\",\"doc_count\":1,\"value\":{\"value\":1},\"key\":1493769600000}"},"doc_count":"1","key":"\"a\""}
  {"date":{"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":2,\"value\":{\"value\":3},\"key\":1493776800000}"},"doc_count":"1","key":"\"a\""}
  {"date":{"buckets":"{\"key_as_string\":\"2017-05-03T00:00:00.000Z\",\"doc_count\":4,\"value\":{\"value\":5},\"key\":1493769600000}"},"doc_count":"4","key":"\"b\""}
  {"date":{"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"},"doc_count":"4","key":"\"b\""}
  </code></pre>
  
  <p>But I only have the last value duplicated in the JsonArray  "ArrayBuckets":</p>
  
  <pre><code>{"date":{"buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"},"doc_count":"4","key":"\"b\""}
  </code></pre>
  
  <p>Here is the Json file :"myjson2.json"</p>
  
  <pre><code>{ 
    "took": 7, 
    "timed_out": false, 
    "_shards": { 
      "total": 5, 
      "successful": 5, 
      "failed": 0 
    }, 
    "hits": { 
      "total": 10, 
      "max_score": 0, 
      "hits": [] 
    }, 
    "aggregations": { 
      "groupe": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
          { 
            "key": "a", 
            "doc_count": 1, 
            "date": { 
              "buckets": [ 
                { 
                  "key_as_string": "2017-05-03T00:00:00.000Z", 
                  "key": 1493769600000, 
                  "doc_count": 1, 
                  "value": { 
                    "value": 1 
                  } 
                }, 
                { 
                  "key_as_string": "2017-05-03T01:00:00.000Z", 
                  "key": 1493776800000, 
                  "doc_count": 2, 
                  "value": { 
                    "value": 3 
                  } 
                } 
              ] 
            } 
          }, 
          { 
            "key": "b", 
            "doc_count": 4, 
            "date": { 
              "buckets": [ 
                { 
                  "key_as_string": "2017-05-03T00:00:00.000Z", 
                  "key": 1493769600000, 
                  "doc_count": 4, 
                  "value": { 
                    "value": 5 
                  } 
                }, 
                { 
                  "key_as_string": "2017-05-03T01:00:00.000Z", 
                  "key": 1493773200000, 
                  "doc_count": 5, 
                  "value": { 
                    "value": 6 
                  } 
                } 
  
              ] 
            } 
          } 
        ] 
      } 
    } 
  } 
  </code></pre>
  
  <p>Here is the result of the code :</p>
  
  <pre><code>{  
     "_shards":"{\"total\":5,\"failed\":0,\"successful\":5}",
     "hits":"{\"hits\":[],\"total\":10,\"max_score\":0}",
     "took":"7",
     "timed_out":"false",
     "aggregations":{  
        "groupe":{  
           "doc_count_error_upper_bound":"0",
           "sum_other_doc_count":"0",
           "buckets":[  
              {  
                 "date":{  
                    "buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
                 },
                 "doc_count":"4",
                 "key":"\"b\""
              },
              {  
                 "date":{  
                    "buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
                 },
                 "doc_count":"4",
                 "key":"\"b\""
              },
              {  
                 "date":{  
                    "buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
                 },
                 "doc_count":"4",
                 "key":"\"b\""
              },
              {  
                 "date":{  
                    "buckets":"{\"key_as_string\":\"2017-05-03T01:00:00.000Z\",\"doc_count\":5,\"value\":{\"value\":6},\"key\":1493773200000}"
                 },
                 "doc_count":"4",
                 "key":"\"b\""
              }
           ]
        }
     }
  }
  </code></pre>