Jill448
Jill448

Reputation: 1793

Groovy: How to parse the json specific key's value into list/array

I am new to groovy and trying 1) from the output of prettyPrint(toJson()), I am trying to get a list of values from a specific key inside an json array using groovy. Using the below JSON output from prettyPrint example below, I am trying to create a list which consists only the values of the name key.

My Code:

def string1 = jiraGetIssueTransitions(idOrKey: jira_id)
echo prettyPrint(toJson(string1.data))

def pretty =  prettyPrint(toJson(string1.data))
def valid_strings = readJSON text: "${pretty}"
echo "valid_strings.name : ${valid_strings.name}"

Output of prettyPrint(toJson(string1.data))is below JSON:

 {
     "expand": "places",
     "places": [
      {
        "id": 1,
        "name": "Bulbasaur",
        "type": {
            "grass",
            "poison"
        }
    },
    {
        "id": 2,
        "name": "Ivysaur",
        "type": {
            "grass",
            "poison"
        }
     } 
    }

Expected result

valid_strings.name : ["Bulbasaur", "Ivysaur"]

Current output

valid_strings.name : null

Upvotes: 1

Views: 10888

Answers (2)

DaveC
DaveC

Reputation: 21

I've used something similar to print out elements within the response in ReadyAPI

    import groovy.json.*
    import groovy.util.*
    
    def json='[
        { "message" : "Success",
            "bookings" : [
                { "bookingId" : 60002172, 
                  "bookingDate" : "1900-01-01T00:00:00" },
                { "bookingId" : 59935582,
                  "bookingDate" : "1900-01-01" },
                { "bookingId" : 53184048, 
                  "bookingDate" : "2019-01-15", 
                  "testId" : "12803798123", 
                  "overallScore" : "PASS" },
                { "bookingId" : 53183765,
                  "bookingDate" : "2019-01-15T13:45:00" },
                { "bookingId" : 52783312,
                  "bookingDate" : "1900-01-01" }
            ] 
          }
    ]
    
    def response = context.expand( json )
    def parsedjson = new groovy.json.JsonSlurper().parseText(response)
    log.info parsedjson
    log.info " Count of records returned: " + parsedjson.size()
    log.info " List of bookingIDs in this response: " + parsedjson.bookings*.bookingId

Upvotes: 1

The pretty printed JSON content is invalid.

If the JSON is valid, then names can be accessed as follows:

import groovy.json.JsonSlurper

def text = """
{
    "expand": "places",
    "places": [{
            "id": 1,
            "name": "Bulbasaur",
            "type": [
                "grass",
                "poison"
            ]
        },
        {
            "id": 2,
            "name": "Ivysaur",
            "type": [
                "grass",
                "poison"
            ]
        }
    ]
}
"""

def json = new JsonSlurper().parseText(text)
println(json.places*.name)

Basically, use spray the attribute lookup (i.e., *.name) on the appropriate object (i.e., json.places).

Upvotes: 2

Related Questions