John Zhang1
Kilo Patron
Kilo Patron

The JSON API has been widely used for ServiceNow. The purpose of this blog is to explore all JSON data-related APIs to learn how their features can be used to manage JSON data through the flow action of a flow designer. This blog will provide you with all useful use cases that can help you with your web service (Rest and Soap) integration as well as Catalog item flow through Flow Designer.

 

Objectives

  1.  Flow Designer JSON APIs and their showcases
    • JSONStreamingBuilder - Scoped
    • JSONStreamingAPI - Scoped
    • JSON 
  2. JSON 3 Conversion Approach Script showcases in Flow Action
    •  Convert Array Of Strings to Array of Objects
    • Convert String to Array of Objects
    • Convert gliderecord to a JSON object
  3. SONv2 web service
  4. GlideJsonPath - Global

 

JSON API

The JSON API has dynamic and static methods. You access the dynamic methods by creating a JSON object. To use the dynamic methods in a scoped application, add the global prefix when calling the constructor. You access the static methods by using the static JSON object.

 

Convert an array of Strings to an array of Objects in action designer.

  • Define input json_arry as a type of JSON in Action Designer

JohnZhang1_0-1698263756169.png

 

  • Define Output variable – list_of_objects is defined on step 1 below

JohnZhang1_1-1698263841464.png

  • Creating the script on step 2 in above

Convert a string to an array of Objects in Action Designer.

  • Define Input comma_delimited_string_list as a type of String in Action Designer

JohnZhang1_0-1698263938732.png

 

  • Define the Output variable - list_of_objects on a step 1

JohnZhang1_1-1698263978647.png

  • Creating the script on step 2 in above

Flow Designer JSON APIs

JSONStreamingBuilder

JSONStreamingBuilder initiates the JSONStreamingBuilder object to build a large streaming JSON payload to use in a REST or SOAP request in the Flow Designer script step with the sn_ih namespace identifier. JSONStreamingBuilder() uses its build() method to return a JSONStreamingAPI object.

 

The JSONStreamingBuilder object has the build() method, which returns a JSONStreamingAPI object. The JSONStreamingAPI object. has the following methods to build the payload:

  • startObject(): Creates the parent JSON object.
  • Methods to generate the JSON key-value pairs, such as writeFieldName(), writeString(), and writeNumberField().
  • endObject(): Closes the parent JSON object.
  • getJSONString() or getAttachmentId(): Returns the JSON string or attachment ID that you created.
  • close(): Closes the JSONStreamingAPI object.

JSONStreamingBuilder Flow Action Use Case 1-Create Json Payload

  • Create new Action in Action Designer
  • Create the following script used within Script Step section to build the Payload:
    1. Create JSONStreamingBuilder object and call .build method
    2. Call startObject() method.
    3. Call eddObject() method.
    4. Call startArray() method
    5. Call endArray() method
  • Define output variable "payload" to hold the created payload.

JohnZhang1_0-1698513136891.png

  • The Action created in Action Designer

JohnZhang1_1-1698513694472.png

 

  • Output (test your action)

JohnZhang1_2-1698513793258.png

SONStreamingBuilder Flow Action Use Case 2-Create the attachment

Create a JSON object and store it in the Attachment [sys_attachment] table with a defined expiration date.

  • Create the following script within the Script section of Action Designer

JohnZhang1_0-1698517711114.png

  • Action created in Action Designer

JohnZhang1_1-1698518012170.png

  • Testing Output- the attachment sysid

JohnZhang1_2-1698518210799.png

 

JSONv2 web service

This is a platform-level processor similar to the services for SOAP, WSDL, CSV, Excel, and XML. Like those services, the JSON web service is triggered by a standalone JSONv2 URL parameter like below

JohnZhang1_0-1698596247758.png

JohnZhang1_1-1698596394322.png

 

GlideJsonPath - Global

The GlideJsonPath API  Instantiates a GlideJsonPath scriptable object by parsing a JSON document like 

JohnZhang1_0-1698596941617.png

Convert a gliderecord to a JSON object Use Case

You can use JSON stringify(Object jsonObject) method to covert gliderecord to JSON object like below:

  • Script Step in Action Designer

JohnZhang1_0-1698855940369.png

  • Output (after running a test)

JohnZhang1_0-1698857019196.png

 

 

 

 

If you enjoy my ServiceNow posts, please be sure to follow me on LinkedIn and my ServiceNow YouTubes Channels

 

6 Comments