groovyjsonbuilder

jsonBuilder without indexes


I tried the example described here: http://docs.groovy-lang.org/2.4.7/html/gapi/groovy/json/JsonBuilder.html

this works great but how can I generate a json payload where some items do not have indexes such as "firstElt" and "secondElt" in this :

[["firstElt","secondElt",[{"thirdElt":{"id":"1","name":"laloune"},"def":"blabla"}]]]

I tried the following:

import groovy.json.JsonBuilder;
def builder = new groovy.json.JsonBuilder()
def root = builder { 
  'root' 'firstElt',
  'secondElt',
  thirdElt(
    id: '1',
    name: 'laloune'
    )
  'def' 'blabla' } 

but it generates the following:

{
   "thirdElt":{
      "id":"1",
      "name":"laloune"
   },
   "root":[
      "firstElt",
      "secondElt",
      {
         "id":"1",
         "name":"laloune"
      }
   ],
   "def":"blabla"
}

Solution

  • I think the comments have essentially answered the question, but as it is sometimes clearer with an example I'll provide one below.

    First you need to keep track of your javascript data types. Javascript has, aside from single valued things, arrays (lists of things) and objects (essentially maps of key value pairs).

    In your example:

    [["firstElt","secondElt",[{...
    ^
    a javascript array
    
    [["firstElt","secondElt",[{...
     ^
     an array within the outer array, index 0
    
    [["firstElt","secondElt",[{...
                             ^
                             a second array at index 1
    
    [["firstElt","secondElt",[{"thirdElt":{...
                              ^
                              a javascript map/object
                              this is the first element of the second
                              array in the outermost array
    

    as mentioned in the comments, most often the simplest way of dealing with this in groovy is to generate a groovy data structure with the relevant array (list in groovy) and object (map in groovy) layout and then just convert it to json. This way you can use all the groovy power for building and mutating (changing) lists and maps and then just produce the json at the end.

    Example code generating the structure in your example:

    import groovy.json.* 
    
    def structure = [             // outermost list
      ["firstElt", "secondElt"],  // a list, structure[0]
      [                           // a list, structure[1]
        [thirdElt: [              // a map, structure[1][0]
          id: "1",                // map entry, structure[1][0]['thirdElt']['id']
          name: "laloune"],
         def: "blabla"            // map entry, structure[1][0]['def']
       ]
      ]
    ]
    
    def json = JsonOutput.toJson(structure)
    def pretty = JsonOutput.prettyPrint(json)
    
    println "json: \n$json"
    println ""
    println "pretty: \n$pretty"
    

    executing this procudes:

    ╭─ groovy-jsonbuilder-without-indexes
    ╰─➤ groovy solution.groovy
    
    json:
    [["firstElt","secondElt"],[{"thirdElt":{"id":"1","name":"laloune"},"def":"blabla"}]]
    
    pretty:
    [
        [
            "firstElt",
            "secondElt"
        ],
        [
            {
                "thirdElt": {
                    "id": "1",
                    "name": "laloune"
                },
                "def": "blabla"
            }
        ]
    ]
    ╭─ groovy-jsonbuilder-without-indexes
    ╰─➤