You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by idioma <co...@gmail.com> on 2016/06/04 23:02:49 UTC

Groovy script to replace Json values via a mapping file

Hi,
I am fairly new to Groovy and I have been struggling with this task for a
while and wondered whether it can be easily achieved by using the
ConfigSlurper. I need to read a mapping file, which looks like the
following:

A some text A2

(I can have some freedom in amending the internal structure of this
properties file)

I have then a JSON file that I read/parse with the JSON Slurper, which
presents the following structure:

{ 
      "field1": "A"
      "field2": "A", 
      "field3": "A" 

 }

We are expecting to get the same value for field1 - field3 every time,
however the mappings should work as follows:

if field1 then no replacement should occur

if field2 has value A then substitute to "some text"

if field3 has value if A then substitute to A2

The mapping file has multiple lines containing different mix of values and
the maps are always conditionally decided according to the value of the
incoming JSON property, which matches to the left hand side value from the
properties file. 

I attempted the following when I initially tried to replace the original
value of field2 on the basis of a modified version of the mappings:

A someText
AF otherText
N3 againSomeText

Here is my attempt: 

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

class LoadConfigFileAndReplaceValues {

    static void main(String[] args) {

        def content = '''
        {
        "field1":"A",
        "field2":"A",
        "field3":"A"
        }
        '''
        def slurper = new JsonSlurper().parseText(content)
        def builder = new JsonBuilder(slurper)

        def propertiesFile = new
File('D:\\Dev\\GroovyTests\\src\\main\\resources\\mappings.txt')
        Properties props = new Properties()
        props.load(new FileInputStream(propertiesFile))
        def conf = new ConfigSlurper().parse(props).flatten();
        
        conf.each { k, v ->
            if (builder.content.field2) {
                builder.content.field2 = v
            }
            println("This prints the resulting JSON :" +
builder.toPrettyString())
        }
    }
}

I am returned with the following:

This prints the resulting JSON :{
    "field1": "A",
    "field2": "someText"
}
This prints the resulting JSON :{
    "field1": "A",
    "field2": "otherText"
}
This prints the resulting JSON :{
    "field1": "A",
    "field2": "againSomeText"
}

whereas I would need to be returned with the first mapping A = someText: 

This prints the resulting JSON :{
    "field1": "A",
    "field2": "someText"
}

I am not sure how I can achieve this and if the ConfigSlurper is the best
approach I can use to do lookups/mappings. Can you please advise? 

Thank you so much,

Ilaria




--
View this message in context: http://groovy.329449.n5.nabble.com/Groovy-script-to-replace-Json-values-via-a-mapping-file-tp5733231.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: Groovy script to replace Json values via a mapping file

Posted by Antonio Antonelli <sh...@gmail.com>.
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 <co...@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.
>

Re: Groovy script to replace Json values via a mapping file

Posted by idioma <co...@gmail.com>.
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.