Home > Mobile >  How to count total number of a specific key from a JSON Object in Groovy?
How to count total number of a specific key from a JSON Object in Groovy?

Time:09-23

I dont have much experience in using JSONSlurper in Groovy and I have below payload:

{
  "header": {
    "quoteType": "YQT",
    "salesOrg": "2040",
    "channel": "02",
    "atpMethod": "01",
    "distributionChannel": "00",
    "division": "00",
    "deliveryDateHeader": "2022-10-08T00:00Z",
    "isCompleteDeliveryRequired": "",
    "shippingCondition": "01",
    "pricingDate": "2022-09-02T08:56Z",
    "currencyCode": "EUR"
  },
  "partner": [
    {
      "partnerNumber": "131761",
      "partnerFunction": "SP"
    },
    {
      "partnerNumber": "131762",
      "partnerFunction": "WE"
    },
    {
      "partnerNumber": "131761",
      "partnerFunction": "RE"
    }
  ],
  "materials": [
    {
      "materialNumber": "29221136",
      "requestedDeliveryDate": "2022-10-08T00:00Z",
      "deliveryGroup": 0,
      "isPartialDeliveryAccepted": "",
      "deliveryPlant": null,
      "deliveryStorageLocation": null,
      "quantity": 1,
      "unitOfMeasure": null,
      "deliveryPriority": null,
      "uomType": "Sales Unit of Measurement"
    },
    {
      "materialNumber": "29233777",
      "requestedDeliveryDate": "2022-10-08T00:00Z",
      "deliveryGroup": 0,
      "isPartialDeliveryAccepted": "",
      "deliveryPlant": null,
      "deliveryStorageLocation": null,
      "quantity": 1,
      "unitOfMeasure": null,
      "deliveryPriority": null,
      "uomType": "Sales Unit of Measurement"
    }
  ]
}

How can I count the total number of "materialNumber"? I have also a code (please check below) but my code does not seem to work as the count is always 0 whenever I run it and I am not also sure on what is wrong.

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper

def Message countMATNR(Message message) {
    def reader = message.getBody(InputStream)
    def jsonIN = new JsonSlurper().parse(reader)

    def property_matnrCounter = 0

    jsonIN.materialNumber.each { materials ->
        if (materials.materialNumber?.trim() && materials.materialNumber != 'null') {
            property_matnrCounter  ;
        }
    }

    message.setProperty("numberOfMaterial", property_matnrCounter)
    return message;
}

CodePudding user response:

You are addressing the wrong properties, the JSON has a list called materials, and each element in the list is an object which contains the key materialNumber!

If you change it to this, then the result is 2.

jsonIN.materials.each { material ->
    if (material.materialNumber?.trim() && material.materialNumber != 'null') {
        property_matnrCounter  ;
    }
}

CodePudding user response:

You can use the out-of-box count() method in a one-liner.

I left irrelevant parts of your json out for brevity.

import groovy.json.JsonSlurper

def json = new JsonSlurper().parseText '''\
{
  "materials": [
    {
      "materialNumber": "29221136",
      "requestedDeliveryDate": "2022-10-08T00:00Z",
      "deliveryGroup": 0,
      "isPartialDeliveryAccepted": "",
      "deliveryPlant": null,
      "deliveryStorageLocation": null,
      "quantity": 1,
      "unitOfMeasure": null,
      "deliveryPriority": null,
      "uomType": "Sales Unit of Measurement"
    },
    {
      "materialNumber": "29233777",
      "requestedDeliveryDate": "2022-10-08T00:00Z",
      "deliveryGroup": 0,
      "isPartialDeliveryAccepted": "",
      "deliveryPlant": null,
      "deliveryStorageLocation": null,
      "quantity": 1,
      "unitOfMeasure": null,
      "deliveryPriority": null,
      "uomType": "Sales Unit of Measurement"
    },
    {},
    {"materialNumber": " "},
    {"materialNumber": "null"},
    {"materialNumber": null}
  ]
}'''

int count = json.materials*.materialNumber.count{ it?.trim() && it != 'null' }

assert count == 2
  • Related