cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

SOLVED
Highlighted
New Contributor

How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

My response api like this in ReadyAPI tool- 

{
"response" : [
{
"imageid" : "asdfg1234",
"softwareId" : 282046477,
"accessLevel" : "register",
},
{"imageid" : "asdfg1235",
"softwareId" : 282046478,
"accessLevel" : "not-register",
},
{
"imageid" : "asdfg1236",
"softwareId" : 282046479,
"accessLevel" : "register",
},

}

I want to store all the values in an arrylist like- 

[ "asdfg1234",282046477,"register","asdfg1235"282046478, "not-register","asdfg1236",282046479,"register"]

I tried this code -  but getting null response

 

ArrayList<String> resArrayList = new ArrayList<String>()
for( int i = 0; i < 10; i++)
{
def resimageid = context.expand( '${RestAPI#Response#$[\'response\'][i][\'imageid\']}' )
def ressoftwareId = context.expand( '${RestAPI#Response#$[\'response\'][i][\'softwareId\']}' )
def resaccessLevel = context.expand( '${RestAPI#Response#$[\'response\'][i][\'accessLevel\']}' )

resArrayList.add(resimageid)
resArrayList.add(ressoftwareId)
resArrayList.add(resaccessLevel)
}
log.info(resArrayList)

 

Please Help me to get the values.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Leader

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

@manisha1 :

 

So, i don't think that will be any change as mongo will return JSON and in Rest response you will be getting json.

 

So use JsonSlurper and match node by node.


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



Thanks and Regards,
Himanshu Tayal

View solution in original post

8 REPLIES 8
Highlighted
Community Hero

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

What are you trying to achieve by doing so?
Use case please?


Regards,
Rao.
Highlighted
New Contributor

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

My requirement is like that - 

1) Store RestAPI Response value in an arrayList.

2) Store DB values in second array List and

3) Compare both the array should return True.

 

 

Highlighted
Community Hero

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

I guess that may not be right way to compare. Because you are putting values into a list and don't know the keys for the same.

Would you please post the sample response of db which needs to be compared?


Regards,
Rao.
Highlighted
Community Leader

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

@manisha1 : For this best approach would be writing your custom groovy code, that would help you. 

 

Use JsonSlurper and pick each node value

Then compare it with DB response using loop for something.

 


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



Thanks and Regards,
Himanshu Tayal
New Contributor

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

I am using MongoDb and values from Db are like that-
[282046477, "asdfg1234","register",282046478,"asdfg1235","not-register",282046479,"asdfg1236","register"]

If  RestAPI Response values stored in [Key,Value] format then comparing both the arryList retun 'False'.

 

please suggest if there is any other solution.

Highlighted
Community Hero

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

I believe the response is an xml from DB. Isn't it?


Regards,
Rao.
Highlighted
Community Leader

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

@manisha1 :

 

So, i don't think that will be any change as mongo will return JSON and in Rest response you will be getting json.

 

So use JsonSlurper and match node by node.


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



Thanks and Regards,
Himanshu Tayal

View solution in original post

Highlighted
Community Manager

Re: How to get all parameter Values from RestAPI response in an arrayList using Java/Groovy

Thank you for looking into this Rao, Himanshu!

 

@manisha1 did the Community help you solve this? Please let us know or simply mark the reply that solved this as a solution🙂


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors