0

I have a query related to how to get the length of a json array in JSR223 Post processor using groovy. There is 2 info blocks inside the "outBound" array. I need to get the length of the array "outBound" so that I can put the length in a for loop. I also want to get an info json array (as it is) which contain the parameter "taxType": "GST". For eg: here 2nd info has GST value.. so wanted to fetch the 2nd info json array

{
      "details": [
        {
          "outBound": [
            {
              "info": {
                "date": "2016-08-11",
                "class": "M",
                "code": 70,
                "pricing": [
                  {
                    "totalAmount": 68.8,
                    "totalTaxAmount": 30.8,
                    "baseFareAmount": 38.0
                  }
                ],
                "totalAmount": 68.8,
                "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "id": 1
          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 68.8,
            "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        },
        {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 78.8,
                "totalTaxAmount": 40.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "id": 2,
            "gstAmount": {
          "taxType": "GST"
        },

          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "200"

            },
             { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        }
      ],
      "resultCount": {
        "count1": 1,
        "count2": 1
      },
      "displayCount": 2
    }
  ]
    }

>Expected Output: 

    {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 78.8,
                "totalTaxAmount": 40.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "id": 2,
            "gstAmount": {
          "taxType": "GST"
            },

          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "200"

            },
             { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        }
Jyothi
  • 1
  • 2
  • The description seems confusing.Can you please explain what are you trying to do? – Abhinandan Satpute Sep 28 '16 at 08:43
  • Do you want to fetch only `info` collection, right?If you fetch only `info` collection it will not give you other part like `segments`? – Abhinandan Satpute Sep 28 '16 at 09:09
  • Thank you so much for Replying back ...First thing I want is to get the length of "outBound" array. You can see the correct structure of the code in json viewer. Second thing is to get the block that contain both "info" and "segment" which has the parameter "taxType": "GST" .. Its like outbound[0] is the first info&segments and outbound[1] is second info &segments. – Jyothi Sep 28 '16 at 10:44

1 Answers1

0

Not quite sure I understood, but if I did this is what you need:

def res = new groovy.json.JsonSlurper().parseText(json)
def outBound = res.details[0].outBound
println outBound.size()
println groovy.json.JsonOutput.toJson(outBound.find { it.info.gstAmount?.taxType == "GST" })

json being a String instance holding your json.

Line by line:

  1. Groovy consumes your Json.
  2. You retrieve the outBound array from the first item of the details array
  3. Printing the size of the outBound array
  4. Formatting back to Json the first element of the outBound array whose info.gstAmout.taxType property is "GST" (the question mark after gstAmount deals gracefully with the possible absence of that property)
sensei
  • 621
  • 7
  • 13