title: Looking for Json-path/(any API) to update any value in given json string in Java
body: <p>Inshort : I am trying to find some api that could just change the value by taking first parameter as jsonString , second parameter as JSONPath and third will be new value of that parameter. But, all I found is this..
  <a href="https://code.google.com/p/json-path/">https://code.google.com/p/json-path/</a></p>
  
  <p>This api allows me to find any value in JSON String. But, I am not finding easy way to update the value of any key. For example, Here is a book.json. </p>
  
  <pre><code>{
  "store":{
      "book":[
          {
              "category":"reference",
              "author":"Nigel Rees",
              "title":"Sayings of the Century",
              "price":8.95
          },
          {
              "category":"fiction",
              "author":"Evelyn Waugh",
              "title":"Sword of Honour",
              "price":12.99,
              "isbn":"0-553-21311-3"
          }
      ],
      "bicycle":{
          "color":"red",
          "price":19.95
      }
     }
   }
  </code></pre>
  
  <p>I can access color of bicycle by doing this.</p>
  
  <pre><code>String bicycleColor = JsonPath.read(json, "$.store.bicycle.color");
  </code></pre>
  
  <p>But I am looking for a method in JsonPath or other api some thing like this</p>
  
  <pre><code>    JsonPath.changeNodeValue(json, "$.store.bicycle.color", "green");
      String bicycleColor = JsonPath.read(json, "$.store.bicycle.color");
      System.out.println(bicycleColor);  // This should print "green" now. 
  </code></pre>
  
  <p>I am excluding these options,</p>
  
  <ul>
  <li>Create a new JSON String. </li>
  <li>Create a JSON Object to deal with changing value and convert it back to jsonstring </li>
  </ul>
  
  <p>Reason: I have about 500 different requests for different types of service which return different json structure. So, I do not want to manually create new JSON string always. Because, IDs are dynamic in json structure. </p>
  
  <p>Any idea or direction is much appreciated. </p>
  
  <p><strong>Updating this question with following answer.</strong> </p>
  
  <ol>
  <li>Copy <a href="https://gist.github.com/vlad-at-yteq/4c42fe4a6874317b81b9#file-mutablejson-java">MutableJson.java</a>.</li>
  <li><p>copy this little snippet and modify as per you need. </p>
  
  <pre><code>private static void updateJsonValue() {
  
  JSONParser parser = new JSONParser();
  JSONObject jsonObject = new JSONObject();
  
  FileReader reader = null;
  try {
      File jsonFile = new File("path to book.json");
      reader = new FileReader(jsonFile);
      jsonObject = (JSONObject) parser.parse(reader);
  
  } catch (Exception ex) {
      System.out.println(ex.getLocalizedMessage());
  }
  
  Map&lt;String, Object&gt; userData = null;
  try {
      userData = new ObjectMapper().readValue(jsonObject.toJSONString(), Map.class);
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  MutableJson json = new MutableJson(userData);
  
  System.out.println("Before:\t" + json.map());
  
  json.update("$.store.book[0].author", "jigish");
  json.update("$.store.book[1].category", "action");
  
  System.out.println("After:\t" + json.map().toString());
  
  }
  </code></pre></li>
  </ol>
  
  <p>Use these libraries. </p>
  
  <ul>
  <li>import org.json.simple.JSONObject;</li>
  <li>import org.json.simple.parser.JSONParser;</li>
  <li>import org.codehaus.jackson.map.ObjectMapper;</li>
  </ul>