groovy convert from csv to json

Your problem here is the use of a GString as key in the map. This looks/prints like a string key, but in terms of identity (how the map looks things up) it is not.

groovy:000> key = "a"
===> a
groovy:000> ["$key": 1].containsKey(key)
===> false
groovy:000> [(key): 1].containsKey(key)
===> true

So use: [(key): vals[i++]] instead.

Or if you want to golf this further:

[keys, line.split(',')].transpose().collectEntries()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top