title: How to modify values of JsonObject
body: <p>I want to add a new field to jsonObject and this new field's name will be based on a value of another field. To be clear, this an examples of what I want to achieve.</p>
  <pre><code>{
    &quot;values&quot;: [
      {
        &quot;id&quot;: &quot;1&quot;,
        &quot;properties&quot;: [
          {
            &quot;stat&quot;: &quot;memory&quot;,
            &quot;data&quot;: 8
          },
          {
            &quot;stat&quot;: &quot;cpu&quot;,
            &quot;data&quot;: 4
          }
        ]
      },
      {
        &quot;id&quot;: &quot;2&quot;,
        &quot;properties&quot;: [
          {
            &quot;stat&quot;: &quot;status&quot;,
            &quot;data&quot;: &quot;OK&quot;
          },
          {
            &quot;stat&quot;: &quot;cpu&quot;,
            &quot;data&quot;: 4
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>I want to add a new field to each json object that will have the value of field &quot;stat&quot; as name.</p>
  <pre><code>{
    &quot;values&quot;: [
      {
        &quot;id&quot;: &quot;1&quot;,
        &quot;properties&quot;: [
          {
            &quot;stat&quot;: &quot;memory&quot;,
            &quot;data&quot;: 8,
            &quot;memory&quot;: 8
          },
          {
            &quot;stat&quot;: &quot;cpu&quot;,
            &quot;data&quot;: 4,
            &quot;cpu&quot;: 4
          }
        ]
      },
      {
        &quot;id&quot;: &quot;2&quot;,
        &quot;properties&quot;: [
          {
            &quot;stat&quot;: &quot;status&quot;,
            &quot;data&quot;: 0,
            &quot;status&quot;: 0
          },
          {
            &quot;stat&quot;: &quot;cpu&quot;,
            &quot;data&quot;: 4,
            &quot;cpu&quot;: 4
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>I have tried to do the following with JsonPath library but for me it's an ugly solution as I will parse the json three times and I do some manual replacements.</p>
  <pre><code>val configuration = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.ALWAYS_RETURN_LIST).build()
  val jsonContext5 = JsonPath.using(configuration).parse(jsonStr)
  val listData = jsonContext.read(&quot;$['values'][*]['properties'][*]['data']&quot;).toString
        .replace(&quot;[&quot;, &quot;&quot;).replace(&quot;]&quot;, &quot;&quot;).split(&quot;,&quot;).toList
  val listStat = jsonContext.read(&quot;$['values'][*]['properties'][*]['stat']&quot;).toString
        .replace(&quot;[&quot;, &quot;&quot;).replace(&quot;]&quot;, &quot;&quot;)
        .replace(&quot;\&quot;&quot;, &quot;&quot;).split(&quot;,&quot;).toList
  // Replacing values of &quot;stat&quot; by values of &quot;data&quot;
  jsonContext5.map(&quot;$['values'][*]['properties'][*]['stat']&quot;, new MapFunction() {
        var count = - 1
        override def map(currentValue: Any, configuration: Configuration): AnyRef = {
          count += 1
          listData(count)
        }
      })
  // replace field stat by its value
  for( count &lt;- 0 to listStat.size - 1){
       val path = s&quot;['values'][*]['properties'][$count]&quot;
       jsonContext5.renameKey(path, &quot;stat&quot;, s&quot;${listStat(count)}&quot;)
  }
      
  </code></pre>
  <p>This is the result obtained</p>
  <pre><code>{
    &quot;values&quot;: [
      {
        &quot;id&quot;: &quot;1&quot;,
        &quot;properties&quot;: [
          {
            &quot;data&quot;: 8,
            &quot;memory&quot;: &quot;8&quot;
          },
          {
            &quot;data&quot;: 4,
            &quot;cpu&quot;: &quot;4&quot;
          }
        ]
      },
      {
        &quot;id&quot;: &quot;2&quot;,
        &quot;properties&quot;: [
          {
            &quot;data&quot;: 0,
            &quot;memory&quot;: &quot;0&quot;
          },
          {
            &quot;data&quot;: 4,
            &quot;cpu&quot;: &quot;4&quot;
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>Is there any better method to achieve this result ? I tried to do it with gson but it's not good handling paths.</p>
  <p>This a way to do it with Gson but I will lose the information about other columns since I'm creating another json.</p>
  <pre><code>val jsonArray = jsonObject.get(&quot;properties&quot;).getAsJsonArray
  val iter = jsonArray.iterator()
  val agreedJson = new JsonArray()
  while(iter.hasNext) {
      val json = iter.next().getAsJsonObject
      agreedJson.add(replaceCols(json))
  }
  def replaceCols(json: JsonObject) = {
      val fieldName = &quot;stat&quot;
      if(json.has(fieldName)) {
        val columnName = json.get(fieldName).getAsString
        val value: String = if (json.has(&quot;data&quot;)) json.get(&quot;data&quot;).getAsString else &quot;&quot;
        json.addProperty(columnName, value)
      }
      json
  }
  </code></pre>