Skip to main content

ServiceNow: Beautiful JSON in the Activity Log

Billy Matthews

Have you ever needed to display a JSON object in a ServiceNow comment or work note? Maybe an integration has failed and we need to convey failure information to a technician.

Easy peasy, let's just do something like current.work_notes = JSON.stringify(myData); right?

Our Example JSON Object

{"id":"card-123","title":"Getting Started with ServiceNow","description":"Learn the basics of ServiceNow platform and how to create your first application","category":"education","tags":["servicenow","beginner","tutorial"],"metadata":{"created":"2025-03-15T10:30:00Z","updated":"2025-03-28T14:45:22Z","author":{"id":"user-456","name":"Jane Developer","email":"jane.dev@example.com"}},"stats":{"views":1245,"likes":89,"shares":32},"isPublished":true,"thumbnailUrl":"https://assets.example.com/images/servicenow-101.jpg","linkUrl":"/courses/servicenow-basics"}

Uhh, okay. It's there but it doesn't exactly look great and the readability is abysmal.

Let's do better. We can use the space parameter to improve our formatting: current.work_notes = JSON.stringify(myData, null, 2);.

Well that didn't really work (but it does in gs.log, try it out!). Now we've got these \n 's everywhere and our spaces seem to have been swallowed by the ether.

Let's leverage HTML, that way we have more control:

current.work_notes = '[code]<pre><code>' + JSON.stringify(myData, null, 2).replaceAll('\n', '<br />') + '</code></pre>[/code]';

And we're there! In this final example, we use the [code] tag to tell ServiceNow we intend to render HTML in this work note, then we use the <pre> tag in HTML to preserve white space, then the <code> tag to semantically inform the browser (and screen readers) that the enclosed is code (visually this tag isn't strictly necessary as omitting it in this context will render the same output), and finally we use .replaceAll('\n', '<br />') to convert our newlines to a format that HTML understands.