To create a list of objects in Groovy from Json, you can use the JsonSlurper class. Here is an example code:
import groovy.json.JsonSlurper
def jsonString = '''[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]'''
def jsonSlurper = new JsonSlurper()
List<Map<String,Object>> objectList = jsonSlurper.parseText(jsonString)
println objectList
In this example, we use the JsonSlurper
class to parse the Json string into a list of maps (List<Map<String,Object>>
). Each map represents an object in the Json array. We then print the list for verification.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-10-31 11:00:00 +0000
Seen: 12 times
Last updated: Apr 12 '21
How to choose specific parameters while selecting inputs in Jenkins (Groovy) Pipeline?
How to modify the node label in Jenkins once the job has been completed successfully?
What are the typical approaches found in a Gradle build script?
How to provide the workspace path as a file to setReportDir in XmlTestRunListener using groovy?
What is the method to invoke a function within Gradle's `pluginManagement {}` section?
What is the reason for the convertibility of variable types in a Groovy class?