0

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"
}
doelleri
  • 19,232
  • 5
  • 61
  • 65
laloune
  • 548
  • 1
  • 9
  • 26
  • 1
    The example you are linking has (close to) no lists in it - but your example has. Please add the code you ran to produce this json to the question. – cfrick Nov 15 '19 at 17:00
  • actually I did not produce this code using groovy, since I try to generate it with jsonBuilder. I guess that my question was confusing – laloune Nov 15 '19 at 17:05
  • 1
    At least for me, the confustion levels are the same. Please add what code you have tried and how it fails to produce the JSON you expect. – cfrick Nov 15 '19 at 17:19
  • 1
    please [edit](https://stackoverflow.com/posts/58881415/edit) the question – cfrick Nov 15 '19 at 18:08
  • 1
    @laloune, put all you need in groovy maps and arrays and convert to json using JsonOutput. http://groovy-lang.org/json.html#_jsonoutput – daggett Nov 15 '19 at 18:35
  • 1
    Have a look at https://stackoverflow.com/questions/13973342/how-to-use-groovy-builder-to-generate-an-array-type-json to generate top level arrays. – cfrick Nov 15 '19 at 19:18

1 Answers1

1

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
╰─➤
Matias Bjarland
  • 4,124
  • 1
  • 13
  • 21
  • thanks a ton for the detailed explanation. this is much clearer now, especially with the comments regarding the access to the elements – laloune Nov 18 '19 at 09:11
  • fyi there was a slight mistake in one of the code comments on the element access, corrected it now. – Matias Bjarland Nov 18 '19 at 09:51