0

I have the following data structure in Groovy

Nested Map Groovy

I want to know if Groovy offers a simple way (Preferably one-liner) to iterate through this data structure, find the TreeMap entry which has a key of "code" and apply the trim() function to it's value. As you can see in the image, the value the key "code" is "1880 ". I want to trim it and make it "1880". Thanks.

user1066568
  • 717
  • 3
  • 15
  • 32

1 Answers1

0

I tried this one and it worked for your data:

ageTypes = ageTypes.collect { it.each { it.value = (it.value instanceof String) ? it.value.trim() : it.value } }

UPD I believe it can be optimized somehow. But need to check how.

Anton Hlinisty
  • 1,441
  • 1
  • 20
  • 35
  • Thanks for the answer Anton! It seems like this code trims all the map values which is not quite what I'm after. As you will notice, when the key is "_links", the value is a LazyMap and I'm getting an exception when it tries to trim a LazyMap. Is there a way to filter out the values so that it trims only strings. – user1066568 Mar 05 '17 at 06:45
  • Sorry, missed the map. Updated the answer with type checking. Looks a bit long. May be it can be somehow shortened. – Anton Hlinisty Mar 05 '17 at 10:02