How to read json file in build.gradle and use string values ​​in build.gradle file

for example, read the json file in build.gradle and use the json values ​​as lines 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 the build.gradle file

+5
source share
1 answer

You can execute any Groovy code from Gradle, and Groovy already has built-in JSON parsers.

eg. you can use a task that will print your value in stdout:

 task parseJson { doLast { def jsonFile = file('path/to/json') def parsedJson = new groovy.json.JsonSlurper().parseText(jsonFile.text) println parsedJson.properties.bar.type } } 
+11
source

All Articles