Json
Purpose¶
Since 6.10.7
Provides methods for manipulating JSON.
Methods¶
Binding name: p6.json
toString¶
Convert the JSON to a stringify and pretty print version (Converted using JsonOutput toJson and prettyPrint)
Syntax
<T extends Serializable> String p6.json.toString(Map<String, T> json)
<T extends Serializable> String p6.json.toString(Map<String, T> json, boolean prettyPrint)
Example
final def json = [names: ['fee', 'foo', 'bar'], values: [1, 2]]
p6.log.info(p6.json.toString json)
final def json = [names: ['fee', 'foo', 'bar'], values: [1, 2]]
p6.log.info(p6.json.toString json, true)
fromString¶
Convert the string to a JSON (Converted using JsonSlurper.parseText)
Syntax
Object p6.json.fromString(String json)
Example
p6.log.info(p6.json.fromString '{"names": ["fee", "foo", "bar"], "values": [1, 2]}')
toXml¶
Convert a json string into xml. (identical to fromJson in xml dsl)
If no root node is in the json, a node named ‘root’ will be added.
Syntax
Object p6.json.toXml(String json)
Example
final def json = """
{
"node1": "xyz",
"node2": "abc"
}
"""
final def xml = p6.json.toXml json
final def validXml = new XmlSlurper().parseText '<root><node1>xyz</node1><node2>abc</node2></root>'
assert xml == validXml
fromXml¶
Convert xml string to json. (identical to toJson in xml dsl)
Syntax
Object p6.json.fromXml(String xml)
Example
final def xml = '<root><node1>xyz</node1><node2>abc</node2></root>'
final def json = p6.json.fromXml xml
final def validJson = [node1: 'xyz', node2: 'abc']
assert json == validJson