cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract and build an array of unique values of a field from an array?

SOLVED
harshabongle
Occasional Contributor

How to extract and build an array of unique values of a field from an array?

I want to extract a set of unique proceduresId values from the response i.e. [211, 324, 167]

Sample response:

{
"result" : {
"propertiesOPC" : [
{
"phasePropertyId" : 3890,
"proceduresId" : 211
},
{
"phasePropertyId" : 3890,
"proceduresId" : 211
},
{
"phasePropertyId" : 3890,
"proceduresId" : 324
},
{
"phasePropertyId" : 3890,
"proceduresId" : 167
},
{
"phasePropertyId" : 3890,
"proceduresId" : 167
}

}

}

1 ACCEPTED SOLUTION

Accepted Solutions
HimanshuTayal
Community Hero

Re: How to extract and build an array of unique values of a field from an array?

@harshabongle :

 

You can use below code to extract only unique elements:

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(res)
def arrSize = jsonSlurper.result.propertiesOPC.size()
HashSet<Integer> uniqueID_set = new HashSet<Integer>();
for(int i = 0 ; i < arrSize ; i++){
	log.info jsonSlurper.result.propertiesOPC[i].proceduresId
	uniqueID_set.add(jsonSlurper.result.propertiesOPC[i].proceduresId)
}
log.info uniqueID_set

 

Hope this will help you in resolving your issue 🙂


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

2 REPLIES 2
HimanshuTayal
Community Hero

Re: How to extract and build an array of unique values of a field from an array?

@harshabongle :

 

You can use below code to extract only unique elements:

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(res)
def arrSize = jsonSlurper.result.propertiesOPC.size()
HashSet<Integer> uniqueID_set = new HashSet<Integer>();
for(int i = 0 ; i < arrSize ; i++){
	log.info jsonSlurper.result.propertiesOPC[i].proceduresId
	uniqueID_set.add(jsonSlurper.result.propertiesOPC[i].proceduresId)
}
log.info uniqueID_set

 

Hope this will help you in resolving your issue 🙂


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

sonya_m
Community Manager

Re: How to extract and build an array of unique values of a field from an array?

Awesome! Thank you for helping, Himanshu!

 

@harshabongle was this the solution you were looking for?


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors