The critical insight with JSON parsing in ServiceNow is that APIs lie — they send malformed JSON, empty responses, or unexpected null values that will crash your script if you don't handle them defensively. Most developers write JSON.parse(response.getBody()) and call it done, then wonder why their Business Rules randomly fail in production. The pattern above handles the three failure modes that will burn you: HTTP errors, empty responses, and malformed JSON. Always wrap in try-catch and never trust nested properties exist.

When to use this pattern

  • When calling external REST APIs that return user data, employee records, or configuration objects you need to parse
  • When building integrations in Business Rules or Scheduled Jobs where you control error handling and logging
  • When the API response structure is known but some fields may be optional or null
  • When you need to extract specific fields from complex nested JSON rather than forwarding the entire response

When NOT to use this pattern

  • Don't parse JSON in Client Scripts — use GlideAjax to call a Script Include that does the parsing server-side
  • Don't use this for ServiceNow's own REST APIs — use GlideRecord or GlideAggregate instead of calling Table API
  • Don't parse massive JSON responses (>10MB) in synchronous scripts — use asynchronous patterns or stream processing
  • Don't use eval() instead of JSON.parse() — it's a security vulnerability that executes arbitrary code

Key behaviors and gotchas

  • Always check HTTP status code before parsing — response.getBody() can return HTML error pages on 404/500 responses
  • Use logical AND (jsonData.field && jsonData.field.subfield) for nested properties to avoid "Cannot read property of undefined" errors
  • Empty response bodies ('' or just whitespace) will cause JSON.parse() to throw "Unexpected end of JSON input"
  • Always validate arrays with Array.isArray() before calling map() or forEach()
  • Log the first 500 characters of malformed JSON in error messages — full responses can flood your system logs
  • Some APIs return 200 OK with error objects in the JSON — check for error or success fields after parsing
⚠️

Never use eval() to parse JSON — it executes any JavaScript code in the response, making your instance vulnerable to code injection attacks. Always use JSON.parse() which only parses data.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime