Write JSON to a file in the current working directory, or to a String.
Fields:json:
The object to write. Can either be a
JSON
instance or another Map/List implementation. Both are supported.
file (optional):
Optional path to a file in the workspace to write to.
If provided, then returnText must be false or omitted.
It is required that either file is provided, or returnText is true.
pretty (optional):
Prettify the output with this number of spaces added to each level of indentation.
returnText (optional):
Return the JSON as a string instead of writing it to a file. Defaults to false.
If true, then file must not be provided.
It is required that either file is provided, or returnText is true.
Example:
Writing to a file:
Writing to a string:
def amap = ['something': 'my datas',
'size': 3,
'isEmpty': false]
writeJSON file: 'data.json', json: amap
def read = readJSON file: 'data.json'
assert read.something == 'my datas'
assert read.size == 3
assert read.isEmpty == false
def amap = ['something': 'my datas',
'size': 3,
'isEmpty': false]
String json = writeJSON returnText: true, json: amap
def read = readJSON text: json
assert read.something == 'my datas'
assert read.size == 3
assert read.isEmpty == false