Forum Discussion

tomaszarmata's avatar
tomaszarmata
Contributor
9 years ago
Solved

Building json using JsonOutput

Hello,

 

I need to produce json looks like:

 

{
    "method": "GET",
    "path": "news",
    "synchronization-type": 2
}

I have tried to do somethink like this:

 

import groovy.json.JsonOutput

def synch = new Synchronize()

synch.method = "GET"
synch.path = "news"
synch.synchronizationType = 2

def json = JsonOutput.toJson(synch)

return JsonOutput.prettyPrint(json)


class Synchronize {

	String method = null
	String path = null
	Integer synchronizationType = null
}

But like You can see there is synchronizationType but I need to map this to synchronization-type. Do You have any suggestions how to do that?

 

 

 

  • Here one way of doing using jackson libraries:

     

    import com.fasterxml.jackson.annotation.JsonProperty
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.databind.SerializationFeature
    
    class Synchronize {
        String method
        String path
        @JsonProperty("synchronization-type")
        int synchronizationType
    }
    
    def synch = new Synchronize(method:'GET', path:'news', synchronizationType:2)
    ObjectMapper mapper = new ObjectMapper()
    mapper.configure(SerializationFeature.INDENT_OUTPUT,true)
    println(mapper.writeValueAsString(synch))

    Get the depenency libraries from here

     

    OUTPUT

    {
      "method" : "GET",
      "path" : "news",
      "synchronization-type" : 2
    }
  • Another simplest way to produce the same output(without having to use java object ) and without additional third party libraries

     

     

    import groovy.json.JsonBuilder
    def json = new JsonBuilder()
    json {
        method 'GET'
        path 'news'
        "synchronization-type" 2
    }
    println json.toPrettyString()

     

    You may also look at the link for array example

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    Here one way of doing using jackson libraries:

     

    import com.fasterxml.jackson.annotation.JsonProperty
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.databind.SerializationFeature
    
    class Synchronize {
        String method
        String path
        @JsonProperty("synchronization-type")
        int synchronizationType
    }
    
    def synch = new Synchronize(method:'GET', path:'news', synchronizationType:2)
    ObjectMapper mapper = new ObjectMapper()
    mapper.configure(SerializationFeature.INDENT_OUTPUT,true)
    println(mapper.writeValueAsString(synch))

    Get the depenency libraries from here

     

    OUTPUT

    {
      "method" : "GET",
      "path" : "news",
      "synchronization-type" : 2
    }
  • nmrao's avatar
    nmrao
    Champion Level 3

    Another simplest way to produce the same output(without having to use java object ) and without additional third party libraries

     

     

    import groovy.json.JsonBuilder
    def json = new JsonBuilder()
    json {
        method 'GET'
        path 'news'
        "synchronization-type" 2
    }
    println json.toPrettyString()

     

    You may also look at the link for array example

    • tomaszarmata's avatar
      tomaszarmata
      Contributor

      Thank You for Your help. I will go in JsonBuilder way.

       

      Sory for my late response.