Home > Enterprise >  Groovy Collect values from nested json arrays
Groovy Collect values from nested json arrays

Time:09-16

I am trying to map a nested json into a flat file but have an issue referencing between different arrays. I get it working for each array separately but can't figure out how to properly reference the parent ids to be included. I tried working with indexes and copying the event.id and event.lots.id on the pricings objects but that got really messy.

Maybe I am just on the wrong track or didn't have the right idea on how this might work.

Code

    def body = message.getBody(String.class)
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(body)
    def i_events = object.events
    def i_lots = object.events.lots
    def i_pricing = object.events.lots.pricings

    def o_values = i_pricing.flatten().collect {"("   "'"   i_events.collect{it.id}[0]   "'"   ","   "'"   i_lots.collect{it.id}[1]   "'"   ","   "'"   it.id   "'"   ","    "'"  it.name   "'"   ")" }.join(',')

    
    //just using print for testing
    println o_values

Result

('event_id1','[id A, id B]','p id1','TEST 1'),('event_id1','[id A, id B]','p id2','TEST 2')

Expected Result

('event_id1','id3','p id1','TEST 1'),('event_id1','id A','p id2','TEST 2')

Sample input

{
  "events": [
    {
      "id": "event_id1",
      "name": "Test Event 01",
      "to": "2021-08-27T02:30:00.000Z",
      "from": "2021-08-26T16:15:00.000Z",
      "parkingTo": "2021-08-27T02:30:00.000Z",
      "parkingFrom": "2021-08-26T14:15:00.000Z",
      "landmarkId": "111",
      "slug": "test-event1",
      "live": true,
      "lots": [
        {
          "id": "id1",
          "name": "Lot 1",
          "pricings": []
        },
        {
          "id": "id2",
          "name": "Lot 2",
          "pricings": []
        },
        {
          "id": "id3",
          "name": "Lot3",
          "pricings": [
            {
              "id": "p id1",
              "name": "TEST 1"
            }
          ]
        }
      ]
    },
    {
      "id": "event_id2",
      "name": "Test Event 2",
      "to": "2020-08-31T17:00:00.000Z",
      "from": "2020-08-31T14:00:00.000Z",
      "parkingTo": "2020-09-01T08:45:00.000Z",
      "parkingFrom": "2020-08-31T12:45:00.000Z",
      "landmarkId": "111",
      "slug": "test-event2",
      "live": true,
      "lots": [
        {
          "id": "id A",
          "name": "lot A",
          "pricings": [
            {
              "id": "p id2",
              "name": "TEST 2"
            }
          ]
        },
        {
          "id": "id B",
          "name": "lot B",
          "pricings": []
        }
      ]
    }
  ],
  "meta": {
    "total": 2,
    "firstElement": 0,
    "lastElement": 2
  }
}

CodePudding user response:

Something like this should work (it's hard to say, as your example input seems different to your expected output)

I added a quote method for if the values contain a ', you will need to think if you need this, and how you're going to escape things

def escape(String s) {
    "'${s.replaceAll("'", "\\\\'")}'"
}

def output = new JsonSlurper().parseText(body).events.collectMany { event ->
    event.lots.collectMany { lot ->
        lot.pricings.collect { pricing ->
            "(${escape(event.id)}, ${escape(lot.id)}, ${escape(pricing.id)}, ${escape(pricing.name)})"
        }
    }
}.join(',')

  • Related