4

I am using curl to get some data, then parsing it with JsonSlurper. The data structure is

"results" : [ 
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-27"
   },
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-27"
   }, 
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-30"
   } 
]

So if I'm not mistaken, the entire thing is considered an object. But there is an array in the object (results) which contains objects in that array.

I need to get the length of the results array. When I try to do json.result.length, I receive a null.

How can I get the length of the results array?

def list = ['curl', '-u', 'user:pass',     "http://localhost:8081/..."].execute()
def json = new JsonSlurper().parseText(list.text)
println json.result.size()
crystallinity
  • 431
  • 2
  • 6
  • 10

3 Answers3

11

What I see is, you are using incorrect property to get the size. Need to use results instead of result. Thats is the reason you are seeing that error as result is null(because there is no such property)

Here is working script and gets output as 3:

import net.sf.json.groovy.JsonSlurper
def jsonText='''{
"results" : [
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-27"
   },
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-27"
   },
   {
      "uri" : "http://localhost:8081/artifactory/api/storage/...",
      "created" : "2015-11-30"
   }
]
}'''
def json = new JsonSlurper().parseText(jsonText) 
println json.results.size()
assert 3 == json.results.size(), "Array size is not matching"
Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thanks so much! First off, I can't believe I had a typo - but it wouldn't have solved my problem because I was trying to use .length. So does .length not work because it's considered a collection and not an array and that's why I need to use .size? Also, is the last line "assert 3 ==..." just error checking? Thanks a ton again, it works perfectly (just trying to understand everything!) – crystallinity Dec 01 '15 at 14:53
  • It happens at times. Right, size is for collection. Nice thread - size vs lengh - http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection. Yes, just checking if results size matching 3. – Rao Dec 01 '15 at 16:11
  • I can't thank you enough for your help. This issue was preventing me from going further with the rest of my code. Thank you thank you thank you! =) – crystallinity Dec 01 '15 at 16:15
2

It will be:

def response = ... // your response as text, stream..
def parsed = new JsonSluper().parse(response) // parseText if string
println parsed.results.size()

BTW: size() is for Collection, length for String, note method vs field. In groovy you can use size() for String as well.

Opal
  • 81,889
  • 28
  • 189
  • 210
0
{
"projects":
  [
  {
    "platform": "java",
    "name":"abc"

  },
  {
    "platform": ".net",
    "name":"abcd"

}]
}

for this json file list.json how to get json size or number of applications count. below code worked for me in groovy as i was using in it my jenkinsfile.

import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.io.FileType
def applicationCount()
{
  jsonFile1 = 'list.json'
  def json1 = readJSON file: jsonFile1
  totalApplication = json1.projects.size()
  println "count" + totalApplication
  println "applicationname" + json1['projects'][0]['name']
}
  • Its actual code that i used in my automation.So yes its copy from my local and pasted in this window.Let me know if you have any question. – JYOTI SINGH Feb 14 '19 at 19:00