How to parse .json file with gradle task and get json data from it?

Is there a way in which I can parse the xyz.json file using the gradle task and get all the individual json data inside? eg. I want to analyze this data, which is stored in the xyz.json file in my resources folder and get all the values ​​inside it, for example. get the value of "type".

{
  "type":"xyz",
  "properties": {
    "foo": {
      "type": "pqr"
    },
    "bar": {
      "type": "abc"
    },
    "baz": {
      "type": "lmo"
    }
  }
}
+4
source share
2 answers

You can create a gradle task like this

gradle myTask{
 doLast{
  def inputFile = new File("xyz.json")
  def json = new JsonSlurper().parseText(inputFile.text)
  def labels = json.properties.foo.type //This will return "pqr"
 }
}
+7
source

Gradle - Groovy. Gradle, , , JsonSlurper json .

-4

All Articles