0

I would like to know how to replace the string inside JSON to another JSON.

 def old = '{"name" : "abc", "value": "123", "field" : "xyz"}'
 def neww = '{"name" : "abc", "value": "345" ,"field" : "xyz"}'

 def old_1 = new JsonSlurper().parseText(old)
 def neww_1 = new JsonSlurper().parseText(neww)

 def commons_slurp = old_1.intersect(neww_1)
 def difference_slurp = old_1.plus(neww_1)
 def final_slurp = difference_slurp.minus(commons_slurp)
 def replace_slurp = old.replace(final_slurp)

 print '\n' +  replace_slurp

and the final output is {"name" : "abc", "345": "123", "field" : "xyz"}

I want the to replace the old value with the new value.

AND, i need that the output would be {"name" : "abc", "value": "345", "field" : "xyz"}

stackerist121
  • 137
  • 2
  • 15

1 Answers1

1

It's much easier:

println groovy.json.JsonOutput.toJson(old_1+neww_1)    
cfrick
  • 35,203
  • 6
  • 56
  • 68
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
  • What do you mean when you say 'two outputs'? In groovy map1 + map2 == map1.plus(map2). Method .plus returns a new Map containing all entries from left and right, giving precedence to right. – Evgeny Smirnov Jan 05 '18 at 08:32