cancel
Showing results for 
Search instead for 
Did you mean: 

how to determine whether json is jsonArray or jsonObject

SOLVED
Highlighted
Regular Contributor

how to determine whether json is jsonArray or jsonObject

Hi All,

 

I am currently stucked at a point where i need to check whether the given json contains array or not.

 

Can any one help me on this:

 

Suppose below is my json:

 

{

"status":"pass",

"code":200,

"data":{

          "abc":[{

          "a1" : "test1"

          "a2" : "test2"

          "a3" : "test3"

          "a4" : "test4"

          },

          {

          "a1" : "test1"

          "a2" : "test2"

          "a3" : "test3"

          "a4" : "test4"

          }],

          "xyz":{

               "b1":"T1"

               "b2":"T2"

               }

     }

}

 

def jsonSlurperTU = new JsonSlurper().parseText(TUResponse)

def expected = jsonSlurperTU.data.abc

def expected1 = jsonSlurperTU.data.xyz

 

i need to check whether xyz is json array or json object as the above json is dummy my json is of 4000 line.

 

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
1 ACCEPTED SOLUTION

Accepted Solutions
Regular Contributor

Re: how to determine whether json is jsonArray or jsonObject

Hi @nmrao ,

 

Thanks for the reply, i will check mentioned solution, however i have done this with below code

 

 

boolean isCollectionOrArray(object) {    
    [Collection, Object[]].any { it.isAssignableFrom(object.getClass()) }
}

// some tests
assert isCollectionOrArray([])

 

For this i imported below library:

 

import java.lang.Object; 

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
5 REPLIES 5
Community Leader

Re: how to determine whether json is jsonArray or jsonObject

From the above JSONs, "abc" is an array and "xyz" is an object. Similarly, "data" is an object.

 

This is the link where you can verify it :-

 

https://jsoneditoronline.org/

 

And below is the link. you can try using code:-

 

https://stackoverflow.com/questions/9988287/test-if-it-is-jsonobject-or-jsonarray/9988392

Regular Contributor

Re: how to determine whether json is jsonArray or jsonObject

Hi @avidCoder ,

 

I know but i want to check this at runtime whether it is jsonarray or jsonobject


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Community Hero

Re: how to determine whether json is jsonArray or jsonObject

@HimanshuTayal 

You can check if the object is instace of a list as shown below:

 

assert (jsonSlurperTU.data.abc instanceof List)
assert false == (jsonSlurperTU.data.xyz instanceof List)


Regards,
Rao.
Regular Contributor

Re: how to determine whether json is jsonArray or jsonObject

Hi @nmrao ,

 

Thanks for the reply, i will check mentioned solution, however i have done this with below code

 

 

boolean isCollectionOrArray(object) {    
    [Collection, Object[]].any { it.isAssignableFrom(object.getClass()) }
}

// some tests
assert isCollectionOrArray([])

 

For this i imported below library:

 

import java.lang.Object; 

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Community Hero

Re: how to determine whether json is jsonArray or jsonObject

No problem. I believe that checking the object instace type is simple. Isn't it?


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors