20

for example, read the json file in build.gradle and use the json values as strings in the file

{
  "type":"xyz",
  "properties": {
    "foo": {
      "type": "pqr"
     },
     "bar": {
       "type": "abc"
     },
     "baz": {
       "type": "lmo"
     }
  }
}

I need to call properties.bar.type and abc should be replaced there.

I need to convert these values to string and use in build.gradle file

PaulShovan
  • 2,140
  • 1
  • 13
  • 22
IMRAN SHAIK
  • 615
  • 2
  • 6
  • 11

1 Answers1

38

From Gradle you can execute any Groovy code and Groovy already has build-in JSON parsers.

E.g. you can use a task that will print your value into stdout:

task parseJson {
    doLast {
        def jsonFile = file('path/to/json')
        def parsedJson = new groovy.json.JsonSlurper().parseText(jsonFile.text)

        println parsedJson.properties.bar.type
    }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40