I need to run the groovy scripts below to build a JSON template. The issue i am running into is that the integer in the template is surrounded in quotes. Stripping out the quotes from the variables treats it like a string.
cat port.txt
1001
Here is my JSON builder script
def test = new groovy.json.JsonBuilder()
test {
ports new File('ports.txt').readLines()*.trim().collect { p ->
[name: "$p-tcp", protocol: "TCP", port: "$p", targetPort: "$p"]
}
}
println test.toPrettyString()
When i run it, it spits out the following:
{
"ports": [
{
"name": "1001-tcp",
"protocol": "TCP",
"port": "1001",
"targetPort": "1001"
}
]
}
However I want it to do strip out the quotes for the ports and targetPorts like so
{
"ports": [
{
"name": "1001-tcp",
"protocol": "TCP",
"port": 1001,
"targetPort": 1001
}
]
}
Any clue on how to accomplish this is much appreciated.