RahulGour
RahulGour

Reputation: 31

How to get end_time and its value in groovy?

How to get end_time and its value in groovy, I'm trying but not getting an output. I want to use the key and the value of the end_time.

   "values": [
                    {
                        "value": {},
                        "end_time": "2021-03-15T07:00:00+0000"
                    }
                ]

Upvotes: 0

Views: 59

Answers (1)

Matias Bjarland
Matias Bjarland

Reputation: 4482

The following code:

import groovy.json.*

def data = '''\
{
"values": [
            {
              "value": {},
              "end_time": "2021-03-15T07:00:00+0000"
            }
          ]
}'''


def json = new JsonSlurper().parseText(data)
def endTimes = json.values.collect { it.end_time }

def firstEndTime = endTimes.first()

println "${endTimes} ${endTimes.getClass()}"
println "${firstEndTime} ${firstEndTime.getClass()}"

extracts first all the end_time values and then the first one of them. When executed, this prints:

─➤ groovy solution.groovy                                                                                          1 ↵
[2021-03-15T07:00:00+0000] class java.util.ArrayList
2021-03-15T07:00:00+0000 class java.lang.String

Upvotes: 3

Related Questions