title: How to manipulate leaves of a JSON tree
body: <p>I want to replace rare words with <code>_RARE_</code> in a JSON tree using <strong>JAVA</strong>.</p>
  
  <p>My rareWords list contains</p>
  
  <pre><code>late  
  populate
  convicts
  </code></pre>
  
  <p>So for JSON below </p>
  
  <pre><code>["S", ["PP", ["ADP", "In"], ["NP", ["DET", "the"], ["NP", ["ADJ", "late"], ["NOUN", "1700&lt;s"]]]], ["S", ["NP", ["ADJ", "British"], ["NOUN", "convicts"]], ["S", ["VP", ["VERB", "were"], ["VP", ["VERB", "used"], ["S+VP", ["PRT", "to"], ["VP", ["VERB", "populate"], ["WHNP", ["DET", "which"], ["NOUN", "colony"]]]]]], [".", "?"]]]]
  </code></pre>
  
  <p>I should get</p>
  
  <pre><code>["S", ["PP", ["ADP", "In"], ["NP", ["DET", "the"], ["NP", ["ADJ", "_RARE_"], ["NOUN", "1700&lt;s"]]]], ["S", ["NP", ["ADJ", "British"], ["NOUN", "_RARE_"]], ["S", ["VP", ["VERB", "were"], ["VP", ["VERB", "used"], ["S+VP", ["PRT", "to"], ["VP", ["VERB", "populate"], ["WHNP", ["DET", "which"], ["NOUN", "colony"]]]]]], [".", "?"]]]]
  </code></pre>
  
  <p>Notice how</p>
  
  <pre><code>["ADJ","late"]
  </code></pre>
  
  <p>was replaced by</p>
  
  <pre><code>["ADJ","_RARE_"]
  </code></pre>
  
  <p>My code so far is like below:</p>
  
  <p>I recursively iterate over the tree and as soon as rare word is found, I create a new JSON array and try to replace the existing tree's node with it. See <code>// this Doesn't work</code> in below, that is where I got stuck. The tree remains unchanged outside of this function.</p>
  
  <pre><code>public static void traverseTreeAndReplaceWithRare(JsonArray tree){   
  
          //System.out.println(tree.getAsJsonArray()); 
  
          for (int x = 0; x &lt; tree.getAsJsonArray().size(); x++)
          {
              if(!tree.get(x).isJsonArray())
              {
                  if(tree.size()==2)
                  {   
                  //beware it will get here twice for same word
                   String word= tree.get(1).toString();  
                   word=word.replaceAll("\"", ""); // removing double quotes
  
                   if(rareWords.contains(word))
                   {
                   JsonParser parser = new JsonParser();                   
  
                               //This works perfectly 
                               System.out.println("Orig:"+tree);
                   JsonElement jsonElement = parser.parse("["+tree.get(0)+","+"_RARE_"+"]");
  
                   JsonArray newRareArray = jsonElement.getAsJsonArray();
  
                               //This works perfectly 
                               System.out.println("New:"+newRareArray);
  
                   tree=newRareArray; // this Doesn't work
                   }                 
  
                  }               
                  continue;   
              }
              traverseTreeAndReplaceWithRare(tree.get(x).getAsJsonArray());
          }
      }
  </code></pre>
  
  <p>code for calling above, I use google's <a href="https://code.google.com/p/google-gson/" rel="nofollow">gson</a></p>
  
  <pre><code>JsonParser parser = new JsonParser();
  JsonElement jsonElement = parser.parse(strJSON);
  JsonArray tree = jsonElement.getAsJsonArray();  
  </code></pre>