I am trying to parse a JSON file using groovy. Json:
{
"Node1": {
"Environment": "NPR",
"OS": "linux",
"Policy_Group": "abc"
},
"Node2": {
"Environment": "NPR",
"OS": "linux",
"Policy_Group": "xyz"
}
}
How can I fetch the values of Environment, OS, Policy_Group using groovy.
I am trying below but its not woring
serverJson[serverLabel].each { serverData ->
print serverData.Environment
print serverData."Environment"
}
where serverlabel
is the Node1
, Node2
and serverJson
is the json file
File file = new File("path_to_file\\example.json");
slurperResponse = new JsonSlurper().parse(file);
slurperResponse.each{
it ->
print (it.getValue().get("Environment"));
print (it.getValue().get("OS"));
}
Here 'it' is an implicit variable. Check documentation
These are the imports you will need;
import groovy.json.JsonSlurper;
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.