The Policy Pub #2: Pretty Print JSON using policy

Blog Post created by macjo14 Employee on Apr 9, 2019

While recently working with some JSON based APIs I needed to display the JSON to review it. What was being returned was all compressed on one line. When this happens with XML I use an XSLT stylesheet to "pretty print" the XML for display. Sadly, we don't have anything equivalent for the JSON world, so I wrote an encapsulated assertion that does the trick. I figured it would be a good one to share with the community.


Attached is the XML for an encapsulated assertion call "Pretty Print JSON" that can be imported via the Tasks -> Extensions and Add-Ons -> Manage Encapsulated Assertions dialogue. 


This will take a string like this:



and make it look like this:


  "nextActions": [
      "action": "NOTIFY",
      "data": {
        "riskAdvice": "INCREASEAUTH",
        "riskScore": 65,
        "deviceId": "IZMEqLcl8oIyplyb1fLmCrXDrHWu9R68bfqWXw5O1faLB1rYsmT6BA=="
  "flowState": "I6eJ06PYy5LUDxy8iSdbQaKgRiG33lAAudqPe9efumSW4ZGFoQUPhO/NfBmYq9fk0Yo7"


The encapsulated assertion takes two inputs: the JSON message as a string (so pass ${<message>.mainpart}) and an optional indentation string (which defaults to two spaces) and returns the pretty-printed JSON as a string in ${json.pretty}. I've tried it against a few sample JSON messages in v9.2+ gateways and it works well.