π§© Working with JSON in ServiceNow: From Basics to Real-World Use Cases!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
β07-20-2025 03:07 AM - edited β07-21-2025 12:11 AM
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
REST API Integration
Parsing external JSON responses into variables
Constructing request bodies using JSON.stringify()
Storing Custom Data Structures
Use in Script Includes to store user preferences, dynamic mappings, etc.
GlideAjax Communication
Send data from server to client as JSON
Flow Designer Scripts
Use JSON when passing structured inputs/outputs between actions
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!**
- 628 Views