The Zurich release has arrived! Interested in new features and functionalities? Click here for more

🧩 Working with JSON in ServiceNow: From Basics to Real-World Use Cases!

Bhavesh Patil
Tera Contributor

Hey #ServiceNowCommunity πŸ‘‹

JSON is everywhere β€” and as ServiceNow developers, we interact with it more often than we realize. Whether you're integrating with external APIs, storing data in variables, or working with complex payloads β€” JSON (JavaScript Object Notation) is a must-know!

In this post, we’ll explore:

βœ…What is JSON and its usage
βœ…How to use JSON in ServiceNow
βœ…JSON.stringify() and JSON.parse()
βœ…Best practices
βœ…Real-time use cases
βœ…Pro tips you don’t want to miss!


πŸ“ŒWhat is JSON?

JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data. It's easy for humans to read and write, and easy for machines to parse and generate.

In ServiceNow, JSON is often used for:

  • Sending/receiving API data (REST/SOAP integrations)

  • Storing complex structures in script fields

  • Passing data between server and client scripts

  • Managing structured records dynamically


πŸ’‘How to Use JSON in ServiceNow

ServiceNow runs on JavaScript, so JSON fits in seamlessly. You’ll mostly use it in Script Includes, Business Rules, Background Scripts, and REST Integrations.

πŸ”„JSON.stringify()

Converts a JavaScript object into a JSON string.

var obj = {
  name: "Bhavesh",
  role: "Developer"
};

var jsonString = JSON.stringify(obj);
gs.info(jsonString); // Output: {"name":"Bhavesh","role":"Developer"}


πŸ”JSON.parse()

Converts a JSON string back into a JavaScript object.

var jsonStr = '{"name":"Bhavesh","role":"Developer"}';
var obj = JSON.parse(jsonStr);
gs.info(obj.name); // Output: Bhavesh

🧠 Note: Always validate your JSON before parsing. A malformed string will throw an error.


βœ…Best Practices

Here are some tried-and-true JSON practices for ServiceNow:

  • βœ…Always validate JSON strings before parsing.

  • βœ…Use JSON.stringify() when storing objects into string fields.

  • βœ…Be careful with large or nested structuresβ€”consider performance and readability.

  • βœ…Use try-catch blocks when parsing external data.

  • βœ…Prefer JSON.stringify(obj, null, 2) for pretty-printing during debugging.


πŸ”Real-Time Use Cases in ServiceNow

  1. REST API Integration

    • Parsing external JSON responses into variables

    • Constructing request bodies using JSON.stringify()

  2. Storing Custom Data Structures

    • Use in Script Includes to store user preferences, dynamic mappings, etc.

  3. GlideAjax Communication

    • Send data from server to client as JSON

  4. Flow Designer Scripts

    • Use JSON when passing structured inputs/outputs between actions

  5. Background Scripts

    • Read large record sets and return them as JSON


βš™οΈQuick Background Script Example

var result = [];
var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.query();

while (inc.next()) {
var obj={};
obj.incNumber = inc.number.toString();
result.push(JOSN.stringify(obj)); } gs.info(result);
gs.info(JSON.parse(result)[0].incNumber); // Logs the JSON string in the system logs

πŸ“ŒThis script returns a JSON structure of all active incident numbers with their descriptions.


πŸš€Pro Tips

🟒 Avoid storing massive JSON structures in string fieldsβ€”it can slow down performance
🟒 Use JSON.stringify(obj, null, 2) in development for readable logs
🟒 For client-side JSON parsing, avoid blocking operations. Always use asynchronous handling (e.g., GlideAjax callback)


🏁Wrapping Up

JSON is the backbone of data interchange in ServiceNow, especially for integrations and dynamic scripting. Mastering it will open doors to advanced workflows, smarter automation, and cleaner code.

Let me know in the comments: Where do YOU use JSON the most in ServiceNow?
Happy scripting! πŸ’»βœ¨

 


**Please mark it as *Helpful* or *Correct* β€” it really means a lot!**

2 REPLIES 2