title: JSON Java add empty array to JSON object
body: <p>I'm stuck with the following problem.</p>
  
  <p>My backend (Java sevlet) returns from a database all the fields from a single record/document from a MongoDB. This JSON string is sent to the frontend where some magic is done.</p>
  
  <p>This is done with the following code:</p>
  
  <pre><code>public String getDocumentJSON(int id) {
      DBCollection collection = database.getCollection("People");
      BasicDBObject query = new BasicDBObject();
      query.put("id", id);
      DBCursor cur = collection.find(query);
      DBObject one = cur.next();  
      return JSON.serialize(one);
  }
  </code></pre>
  
  <p>The frontend requires a field: <code>photos[]</code></p>
  
  <p>New records/documents are created with this field. (it's an array with photo filenames). The older records/documents do not have this field, which causes the frontend to break.</p>
  
  <p>To prevent this I would want to add the array: <code>photos[]</code> to the JSON before sending it to the frontend. But I'm not to sure how I add this array to the JSON.</p>
  
  <p>Preferably my code would check to see if the JSON or DBObject has the field <code>photos[]</code> if it does, then nothing is done, if it doesn't contain this field. Then it is added.</p>
  
  <p>The frontend just needed the <code>photos[]</code> even if it's empty that's no problem.</p>
  
  <p>I would rather fix this in the backend that in the frontend.</p>