Count items in a JSON object using Groovy

Viewed 668

My JSON file

{
    "General": [
        {
            "AppUrl": "abc.com/",
            "HTTPResponseCode": "200",
            "QA Build Version#": "4.3.44"
        }
    ]
}

I want to get the count elements(key-value pairs) of the above JSON object.

I tried with the below code but it returns a count as 1.

My code

def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.size()
1 Answers

Instead of:

def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.size()

Should be:

def InputJSON = new JsonSlurper().parse(new File(fileName))
def count = InputJSON.General.keySet().size()
Related