Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Compare and replace string in JSON using Groovy [duplicate]

Elmo Tabuzo:

This question already has an answer here:

  • Does Groovy have method to merge 2 maps? 1 answer

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 =
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"}

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Compare and replace string in JSON using Groovy [duplicate]


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription