Skip to content

Conversation

@vhudlikar
Copy link
Contributor

JSON/XML Data Transformer

This Pull Request introduces a custom Flow Designer Action named JSON/XML Data Transformer. This utility is designed to standardize the extraction of single data points from complex, unstructured JSON or XML payloads (typically from REST or SOAP API responses) into a reusable and fail-safe Flow variable. It is built to be resilient within strict Scoped Application environments.

This action solves the critical integration challenge of robustly handling external API response bodies without relying on complex, repeated Script Steps.

Problem Solved Benefit
Flow Failures Eliminates flow crashes when an expected field is null or missing, by returning a customizable Default Value instead.
Code Abstraction Replaces dozens of lines of redundant JSON parsing and XPath code with a single, reusable action, significantly cleaning up Flow logic.

Primary Use Case: Directly follows a REST or SOAP step in Flow Designer to safely extract a status code, an ID, or a specific error message from the response body.

Action Dynamics and Usage
This action is dynamic because both the raw data and the extraction path are provided as string inputs (data pills), allowing the path to be constructed dynamically if needed.

Input Field Type Description
Raw Data String String The entire JSON or XML response body string.
Data Format Choice
(JSON/ XML)
Specifies the parsing logic to be applied.
Extraction Path String The path to the desired value (e.g., user.id or /root/user/@id).
Default Value String The value to return if the extraction_path fails to find data.

Action Outputs

Output Field Type Description
Extracted Value String The found value or the default_value.
Success True/False True if a value was found at the path, False otherwise.
Error Message String Contains details on parsing or path failure.

Logic Implementation Details
The Action contains a single Script Step named Parse and Traverse Data that uses format-specific logic, specifically engineered to be resilient within the constraints of Scoped Application Flow Designer Script Steps.

JSON Logic (Dot Notation / Array Indexing)

  • Parser: Uses the most resilient, scope-safe parser available: global.JSON.parse().
  • Traversal: Custom JavaScript logic is used to traverse the parsed object using dot notation (user.name) and supports array indexing (items[1]).
  • Resilience: If traversal fails, it guarantees the default_value is returned, and success is set to False.

XML Logic (XPath)

  • Parser: Directly utilizes the server-side class XMLDocument2() combined with the .parseXML() method.
    • Note: This approach bypasses restrictions imposed on other XML classes (like XMLDocument), confirming scope compatibility on this instance.
  • Extraction: Uses the direct and simple XPath evaluation method xmlDoc.getNodeText(xpath). This returns the text content of the first node matching the provided XPath (e.g., /root/status).
  • Resilience: If xmlDoc.getNodeText() returns null (meaning the path was not found), it explicitly returns the default_value and sets success to False.

Error Handling
The generic catch block employs the resilient method e.toString() to capture any error object including difficult-to-read, restricted Java Exception objects often thrown in Scoped Apps. This ensures a clean, readable error message is always captured in the error_message output, rather than triggering a secondary security exception or returning an unreadable object.

Detailed Test Cases for Validation
These tests confirm successful extraction, proper use of the default value, and robust error handling for malformed inputs in a Scoped Application environment.

JSON Test Cases (Format: json)
The extraction_path uses Dot Notation and Array Indexing (e.g.,user.location.city, items[1])

Test Type Raw
Data String
(Input)
Extraction Path Default Value Expected Extracted
Value
Expected Success Expected Error
Message
Positive 1 (Simple) {"status": "Ready"} status N/A "Ready" True (Empty)
Positive 2 (Nested) {"user": {"profile": {"email": "[email protected]"}}} user.profile.email N/A "[email protected]" True (Empty)
Positive 3 (Array Index) {"items": ["Alpha", "Beta", "Gamma"]} items[1] N/A "Beta" True (Empty)
Negative 1 (Missing Path) {"data": 1} missing.path "UNKNOWN" "UNKNOWN" False JSON path "missing.path" not found. Returning default value.
Negative 2 (Malformed) {"data": 1, data "ERROR" "ERROR" False FATAL PARSING ERROR (json): SyntaxError: Unexpected end of input (or similar)

XML Test Cases (Format: xml)
The extraction_path uses strict XPath syntax (e.g., /root/node or //node[1]).

Test Type Raw Data String
(Input)
Extraction Path
(XPath)
Default Value Expected Extracted
Value
Expected Success Expected Error
Message
Positive 1 (Absolute Path) <resp><id>101</id></resp> /resp/id N/A "101" True (Empty)
Positive 2 (Nested) <cfg><db><host>srv1</host></db></cfg> /cfg/db/host N/A "srv1" True (Empty)
Positive 3 (Forgiving Path) <list><item>X</item></list> //item N/A "X" True (Empty)
Negative 1 (Missing Path) <config><data>1</data></config> /config/tag "NONE" "NONE" False XML XPath "/config/tag" not found. Returning default value.
Negative 2 (Malformed) This is not xml //any "ERROR" "ERROR" False FATAL PARSING ERROR (xml): JavaException: org.xml.sax.SAXParseException... (or similar parsing error)

@github-actions
Copy link

Valid PR for ActionPack

Thank you for your contribution. This PR complies with the CONTRIBUTING.md.
A maintainer will review this shortly. In the meantime, Happy Hacking!

@ravichandra1998g ravichandra1998g self-assigned this Oct 19, 2025
Copy link
Contributor

@ravichandra1998g ravichandra1998g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the detailed PR description

@ravichandra1998g ravichandra1998g merged commit e7c92f4 into ServiceNowDevProgram:main Oct 19, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants