[task] Json transformation
Description
jsont is json transformer task that allow to:
- read properties from json file(s)
- process properties using freemarker templates
Jsont task parameters
Attribute | Description | Value | Required |
---|---|---|---|
file | input file | .json | yes |
tofile | output file | . | yes |
template | freemarker template | .ftl | yes |
overwrite | overwrite output file | true by default | no |
Examples
Rewrite json file
file.json
- input file
{
"id": 123,
"name": "Pankaj",
"permanent": true,
"address": {
"street": "Albany Dr",
"city": "San Jose",
"zipcode": 95129
},
"phoneNumbers": [
123456,
987654
],
"role": "Manager",
"cities": [
"Los Angeles",
"New York"
],
"properties": {
"age": "29 years",
"salary": "1000 USD"
}
}
build.xml
- ant file:
<project name="bidji-jsont" xmlns:bj="antlib:org.bidji.taskdefs">
<target name="rewrite" description="rewrite json">
<bj:jsont file="file.json" tofile="res.json" template="rewrite.ftl" overwrite="true"/>
</target>
</project>
rewrite.ftl
- freemarker file
{
[#foreach property in json?api.properties()]
[#if property.is_string()]
"${property.key()}": "${property.as_string()}"[#if !property?is_last],[/#if]
[#elseif property.is_number()]
"${property.key()}": ${property.as_number()}[#if !property?is_last],[/#if]
[#elseif property.is_boolean()]
"${property.key()}": ${property.as_boolean()?c}[#if !property?is_last],[/#if] [#-- or myBool?string('yes', 'no') --]
[#elseif property.is_object()]
"${property.key()}": ${property.as_object()}[#if !property?is_last],[/#if]
[#elseif property.is_array()]
"${property.key()}": ${property.as_array()}[#if !property?is_last],[/#if]
[/#if]
[/#foreach]
}