In our automation framework, we are handling the test data in the JSON file. below is the sample file
{
"Sheet1": [
{
"id": "1",
"firstname": "test",
"lastname": "last",
"email": "jpenddreth0@census.gov",
"gender": "female",
"ip_address": "26.58.193.2"
},
{
"id": "2",
"firstname": "test2",
"lastname": "last2",
"email": "gfrediani1@senate.gov",
"gender": "male",
"ip_address": "229.179.4.212"
}
]
}
first, we convert the JSON file into MapObject, from the map we will read the data.
def getObjectKeyValueInTheMapList(fileName,objectName,key) {
def keyValue = []
Map jsonMap = new LinkedHashMap()
jsonMap = converJsonToMapObject(fileName)
def getKey = jsonMap.get(objectName)
}
getKey return
[[email:jpenddreth0@census.gov, firstname:test, gender:female, id:1, ip_address:26.58.193.2, lastname:last], [email:gfrediani1@senate.gov, firstname:test2, gender:male, id:2, ip_address:229.179.4.212, lastname:last2]]
from the above map, how can I get the email
key value for id =1
ie if id = 1
then I want to read the email / firstname /lastname
Check this out. In Groovy you can define a map like this def jsonMap = [:]
. You can get the values in many ways for example some of them: jsonMap.email
or jsonMap.['email']
will return jpenddreth0@census.gov
.
You have a list of maps and the goal is to find the map with the id==1
. If you have to do that more often, then the best approach is to shape your data in a better way to access them by id
.
Eg build up a map of users by id
with something like
def usersById = data.collectEntries{ [it.id, it] }
Then usersById[1]
will give you the map with the id==1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.