groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Antonio Antonelli <sh4d0...@gmail.com>
Subject Re: Groovy script to replace Json values via a mapping file
Date Tue, 07 Jun 2016 23:50:01 GMT
Hi Ilaria,

did not quite understand what you are trying to do but for sure you are
messing up passing the slurper to the builder. As far as I understood you
want to replace values or delete them (A2?) from a json so better work on
maps:

def json = '''
        {
        "field1":"A",
        "field2":"A",
        "field3":"A"
        }
        '''

def mapping = '''
A="someText"
AF="otherText"
N3="againSomeText"
'''
def slurper = new JsonSlurper().parseText(json)
def conf = new ConfigSlurper().parse(mapping)

def map = slurper.collectEntries { k, v ->
    if (k == 'field2' && conf[v]) v = conf[v]
    if (k == 'field3' && conf[v]) return [:] // what do you mean with
"if field3 has value if A then substitute to A2" ??
    [(k): v]
}

new JsonBuilder(map).toPrettyString()

note the mapping format is different from the one you have. I don't think
the conifgslurper can be configured to understand yours.
For questions like this you should you stackoverflow, you may find more
people willing to help you.

Cheers,
Antonio

On Tue, 7 Jun 2016 at 23:15 idioma <corda.ilaria@gmail.com> wrote:

> Hi,
> I was hoping to get some feedback from this community on how my attempt can
> be improved in any way or whether I am actually heading towards the wrong
> direction.
>
> Thank you so much,
>
> I.
>
>
>
> --
> View this message in context:
> http://groovy.329449.n5.nabble.com/Groovy-script-to-replace-Json-values-via-a-mapping-file-tp5733231p5733272.html
> Sent from the Groovy Users mailing list archive at Nabble.com.
>

Mime
View raw message