title: How to accept dynamic json response and add one key-value to that object and return that same JSONObject from the spring boot controller
body: <p>My controller is receiving post request as dto object and my dto has one Object array which is used to get the array of json objects, I have to add one key-value to each json object in that array and return it back.</p>
  
  <p>dto class:</p>
  
  <pre><code>public class FileProcessDTO {
  
      private String module;
      private Object[] data;
  
      public String getModule() {
          return module;
      }
      public void setModule(String module) {
          this.module = module;
      }
      public Object[] getData() {
          return data;
      }
      public void setData(Object[] data) {
          this.data = data;
      }
  }
  </code></pre>
  
  <p>below is controller method</p>
  
  <pre><code>@PostMapping("/processData")
      public FileProcessDTO processFileData(@Valid @RequestBody FileProcessDTO fileProcess) throws JSONException {
  
          String module = fileProcess.getModule();
          Object[] objects= fileProcess.getData();
  
          JSONObject jsonObject = new JSONObject();
          JSONArray jsonArray = new JSONArray(fileProcess.getData());
  
          FileProcessDTO fileProcessDTO = new FileProcessDTO();
          fileProcessDTO.setModule(module);
  
  
          for (int i = 0; i &lt; jsonArray.length(); i++) {
              try {
                  jsonObject = jsonArray.getJSONObject(i);
                  jsonObject.put("status", true);
              } catch (Exception e) {
                  jsonObject.put("status", false);
                  jsonObject.put("error", e.getMessage());
              }
  
  
              objects[i]=jsonObject;
  
          }
          fileProcessDTO.setData(objects);
  
          return fileProcessDTO;
      }
  </code></pre>
  
  <p>and below is the data passed in post request from client</p>
  
  <pre><code>{
  "module" : "assignment",
  "data":[
      {
          "name": "chandan",
          "age":"27"
      },
      {
          "name": "Yo",
          "age":"26"
      },
      {
          "name": "Jo",
          "age":"25"
      }
  ]
  }
  </code></pre>
  
  <p>In above objects of <em>"data"</em> array keys and values will be different in different request(may be in next request <em>"name" and "age"</em> will changed).</p>
  
  <p>Below I am getting error.</p>
  
  <pre><code>org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.boot.configurationprocessor.json.JSONObject]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.boot.configurationprocessor.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.crisil.apg.service.dto.FileProcessDTO["data"]-&gt;org.springframework.boot.configurationprocessor.json.JSONObject[0])
  2019-04-17 12:04:14.002  WARN 10808 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator
  </code></pre>
  
  <p>I also tried to change line in controller method as below but, its not as json response.</p>
  
  <pre><code>objects[i]=jsonObject.toString();
  
  //below is response
  {
      "module": "assignment",
      "data": [
          "{\"name\":\"chandan\",\"age\":\"27\",\"status\":true}",
          "{\"name\":\"Yo\",\"age\":\"26\",\"status\":true}",
          "{\"name\":\"Jo\",\"age\":\"25\",\"status\":true}"
      ]
  }
  </code></pre>
  
  <p>Kindly suggest the possible code to make it work.Thanks.</p>