title: How to modify the JSON data and return the updated JSON data
body: <p>We have a requirement to update the JSON data in middle and need to return the updated JSON data using java. Also it should support any type of JSON data.</p>
  
  <p>ex: 
  Assume <code>{object:{"color":"red","shape":"Triangle"}}</code> is the JSON data and in this we need to update the <code>shape</code> value to <code>Rectangle</code> and we need to return the updated JSON data as below:</p>
  
  <pre><code>{object:{"color":"red","shape":"Rectangle"}}
  </code></pre>
  
  <p>For this we need to pass the element path ( which element we need to update) and updateText and JSON Data to the JAVA code.</p>
  
  <p>here is the methodCall:</p>
  
  <pre><code>updateValue("object/shape", "Rectangle", "{object:{"color":"red","shape":"Triangle"}}")
  </code></pre>
  
  <p>We tried below code using Gson library. But with this code we are able to update the targeted Json element, but the requirement is to return the  entire JSON data with the updated value. </p>
  
  <p>So please suggest how do we re-build the JSON data with the updated text.</p>
  
  <p>Below is the code we tried to update the Json Data.</p>
  
  <pre><code>    public String updateValue(String keyPath, String updateText, String jsonText) {
      String[] keys = keyPath.split("/");
      JsonParser jsonParser = new JsonParser();
      JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
      String result = "";
      for(String key : keys)
      {
          if (jsonObject.get(key) instanceof JsonObject)
          {
            jsonObject = (JsonObject)jsonObject.get(key);
          }
          else if(jsonObject.get(key) instanceof JsonArray)
          {
            JsonArray jsonArray = (JsonArray)jsonObject.get(key);
            result = jsonArray.toString();
          }
          else 
          {
            result = jsonObject.get(key).toString();
          }           
      }
      result = result.replace(result, updateText);
      return result;      
  }
  </code></pre>