Formatting Objects in Scripts for Readability

Formatting Objects in Scripts for Readability

When outputting objects in logs or in print lines in Tryout for debugging purposes it can be useful to have them in a more easily human-readable format

Groovy has good native support for this with most data types (there are probably some exceptions!)

JsonBuilder.toPrettyString()

It is as simple as this one line of code in most cases:

new groovy.json.JsonBuilder( [ ... dataToVisualise ... ] ).toPrettyString()

For example:

// Find all "flow" events from the dialog history
def allFlowEvents = []
_.dialogHistory?.each { transaction ->
    allFlowEvents << transaction.path.findAll { it.type.endsWith("Flow") }
}

// Write them to engine logs
println new groovy.json.JsonBuilder(allFlowEvents).toPrettyString()

Will output something like this

03/02/2020 15:52:39 [Println]: [
    [
        {
            "metadata": {
                "Set on Flow": "tracing_var"
            },
            "vertexName": "",
            "vertexId": "f36a5ed0-2d4a-4a5a-872f-f223c73e2d58",
            "flowFolder": "/Folder 1",
            "changedSessionVariables": {
                
            },
            "name": "Normal flow",
            "description": "",
            "changedFlowVariables": {
                
            },
            "type": "raiseFlow",
            "flowId": "95dd1117-9a42-4513-9730-6f11fc78dacb",
            "flowName": "Normal flow",
            "flowInstanceId": "bb705b2c-f2fd-4901-b4ca-33a86831c7fe"
        },
        {
            "metadata": {
                "Set on Flow": "tracing_var"
            },
            "vertexName": "",
            "vertexId": "0df4a444-9447-4165-a7ac-7ded635943ae",
            "aborted": false,
            "description": "",
            "type": "dropFlow",
            "flowName": "Normal flow",
            "flowInstanceId": "bb705b2c-f2fd-4901-b4ca-33a86831c7fe",
            "flowFolder": "/Folder 1",
            "stuck": false,
            "changedSessionVariables": {
                
            },
            "name": "Normal flow",
            "changedFlowVariables": {
                
            },
            "flowId": "95dd1117-9a42-4513-9730-6f11fc78dacb"
        }
    ]
]

Instead of this!

3/02/2020 15:57:28 [Println]: [[[metadata:[Set on Flow:tracing_var], vertexName:, vertexId:f36a5ed0-2d4a-4a5a-872f-f223c73e2d58, flowFolder:/Folder 1, changedSessionVariables:[:], name:Normal flow, description:, changedFlowVariables:[:], type:raiseFlow, flowId:95dd1117-9a42-4513-9730-6f11fc78dacb, flowName:Normal flow, flowInstanceId:7313d738-aa20-4c69-bf15-7812316df1b0], [metadata:[Set on Flow:tracing_var], vertexName:, vertexId:0df4a444-9447-4165-a7ac-7ded635943ae, aborted:false, description:, type:dropFlow, flowName:Normal flow, flowInstanceId:7313d738-aa20-4c69-bf15-7812316df1b0, flowFolder:/Folder 1, stuck:false, changedSessionVariables:[:], name:Normal flow, changedFlowVariables:[:], flowId:95dd1117-9a42-4513-9730-6f11fc78dacb]]]
1 Like