One of the upcoming features of the Dublin Release at ServiceNow is an updated JSON Web Service processor. While it is still not an official REST API for the platform, it is a huge improvement on the original JSON processor that was release a few years ago.
Why the Update
The original JSON Web Service was created to address some basic needs that a handful of customers were requesting in order to use simple RESTful calls to interact with the ServiceNow platform. However, there were some serious performance issues and bulk data limitations that hindered the processor. Secondly, the original processor had some slight security issues that made the processor a risk to some organizations based on their use case. Lastly, the JSON Web Service was inconsistent with how its counterparts worked (eg. JSON web service issue with Web Service Import sets).
Due to the underlying issues in the JSON web service plugin, ServiceNow rewrote the plugin from scratch and worked to get it more similar to the capabilities our SOAP Web Services as well as our other CSV and XML processors for handling bulk data.
Starting with Dublin, ServiceNow will enable the JSONv2 plugin by default. They will also deprecate (and remove) the original JSON web service plugin (can possibly be loaded on special request). Additionally, the processor is now being moved to the Java code rather than using the Javascript implementation.
Rumor has it that the JSONv2 plugin will be made available for Calgary instances in the next publicly released Calgary Patch.
I highly recommend customers discontinue the use of the JSON Web Service plugin and migrate those integrations to be compatible with the JSONv2 format (not much has changed in the interface…so this shouldn’t be too hard).
General Information
The JSONv2 Web service will be accessed by using the URL parameter: “JSONv2” (instead of the “JSON” parameter used by the original processor).
The JSONv2 Web Service can use either GET or POST requests for querying or deleting data. POST requests are required for creating or modifying data through the web service.
Like its predecessor, JSONv2 web service goes through a Basic Authentication layer for determining a client’s access to the resources being referenced.
A “Content-Type” header is now required on all requests. This will be important (and easy) to implement as you migrate old JSON web service clients over to JSONv2.
Using the JSONv2 Web Service
1) Set up the base URL
2) Add the JSONv2 parameterIn order to access a table’s resources through JSONv2, you must reference the table URL that you are going to work with.
In ServiceNow, you can access a table resource by appending it’s database name to the end of the URL and then adding a “.do” exention. For example, the “Incident” table in ServiceNow has a Database Name of “incident”. You would form that URL in this manner:
https://MYINSTANCE.service-now.com/incident.doFor the “ECC Queue” table (ecc_queue), you would use:
https://MYINSTANCE.service-now.com/ecc_queue.do
This is as simple as adding "JSONv2" or "JSONv2=true" to your URL. So, if you were accessing the incident table, your URL might look like:https://MYINSTANCE.service-now.com/incident.do?JSONv2
3) Set your Content-Type Header
Remember that this header needs to be set. It is always going to have the same value, but you have to include it. Your header string would look something like:
Content-Type: application/json
4) Set your desired action
You can do the following actions with the Web Service (see the official documentation for definitions on what these actions do): get, getKeys, getRecords, insert, insertMultiple, update, deleteRecord, deleteMultiple.
If we wanted to do a getRecords call, we would set up our sysparm_action parameter in the following way:
https://MYINSTANCE.service-now.com/incident.do?JSONv2&sysparm_action=getRecords
5) Add additional parameters as needed
Depending on what you want to do, you will need/want additional URL parameters. The ones most commonly used by me are as follow:
- displayvalue (true, false, or all - if true, it returns display values instead of sys_id's for reference fields. If all, it returns BOTH the display value and the sys_id of reference fields)
- sysparm_view (a view name - limits what fields are returned with the record data - used with "getRecords")
- sysparm_query (an encoded query to filter records - used with "getKeys", "getRecords", "deleteMultiple"
- sysparm_record_count (limits amount of records returned - used with "getRecords" only)
- sysparm_sys_id (used with "get" or "deleteRecord" only)
6) Add body content for the POST
If you are performing an insert, insertMultiple or an update, you will need to post the data in JSON format as the Body of the HTTP POST request. For more information on the format, see the wiki documentation for JSONv2 Data Modification.
Example usage of the JSONv2 Web Service
In the following example, we are going to query the incident table of an instance. Since we may have more incident records that are allowed, we are going to limit the number of returned records and chuck the requests. Also, we don't care about a lot of the fields, so we are only going to require the fields from the "mobile" view to be returned through the web service calls. Finally, we will request display values rather than sys_ids for reference fields.
Let's follow our steps to generate the first RESTful request:
1) Set up the URL to operate against the incident table
https://MYINSTANCE.service-now.com/incident.do
2) Invoke the JSONv2 Processor
https://MYINSTANCE.service-now.com/incident.do? JSONv2
3) Set the Content-Type to "application/json"
4) Specify the "getRecords" action
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords
5) Use some optional parameters to get what we want
We will ask for display variables to be returned for reference fields:
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords &displayvalue=true
We will restrict the number of fields to just those of the mobile view:
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords &displayvalue=true &sysparm_view=mobile
We will want to order the results by sys_id since we are going to page through them:
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords &displayvalue=true &sysparm_view=mobile &sysparm_query=ORDERBYsys_id
We will page the data to manageable sets. For this example, we will say "3" records. That is really low, but this is just a demo:
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords &displayvalue=true &sysparm_view=mobile &sysparm_query=ORDERBYsys_id &sysparm_record_count=3
1 | https://MYINSTANCE.service-now.com/incident.do?JSONv2&sysparm_action=getRecords&displayvalue=true&sysparm_view=mobile&sysparm_query=ORDERBYsys_id&sysparm_record_count=3 |
This will get us the following response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | { "records": [ { "__status": "success", "vendor_point_of_contact": "Whale", "caller_id": "Celia Slavin - Test", "work_notes": "", "vendor": "IBM", "state": "Closed", "sys_id": "06a4b716eb920100a9e7e26ac106feb6", "number": "INC0003027", "assigned_to": "ITIL User - Test", "category": "Inquiry / Help", "short_description": "IBM Cloud Computing is experiencing turbulence.", "priority": "2 - High", "vendor_ticket": "IBM20203", "assignment_group": "Service Desk", "comments": "2013-10-27 09:11:49 - System Administrator (Additional comments)\nThe incident has been resolved.\n\n" }, { "__status": "success", "vendor_point_of_contact": "Tom", "caller_id": "Dionne Borycz - Test", "work_notes": "", "vendor": "Amazon", "state": "(-8)", "sys_id": "135df534df120100cd7da5f59bf26302", "number": "INC0003005", "assigned_to": "ITIL User - Test", "category": "Inquiry / Help", "short_description": "Amazon EC2 appears to be down", "priority": "2 - High", "vendor_ticket": "AMZ41103", "assignment_group": "Service Desk", "comments": "" }, { "__status": "success", "vendor_point_of_contact": "Watson", "caller_id": "Nelly Jakuboski - Test", "work_notes": "", "vendor": "IBM", "state": "(-8)", "sys_id": "1a0db534df120100cd7da5f59bf26304", "number": "INC0003004", "assigned_to": "ITIL User - Test", "category": "Inquiry / Help", "short_description": "Cloud computing db down", "priority": "5 - Planning", "vendor_ticket": "IBM88108", "assignment_group": "Service Desk", "comments": "" } ] } |
Those are the first three records. We are then going to take the sys_id from the last record and query for the next set of records with the following URL:
https://MYINSTANCE.service-now.com/incident.do? JSONv2 &sysparm_action=getRecords &displayvalue=true &sysparm_view=mobile &sysparm_query=sys_id>1a0db534df120100cd7da5f59bf26304^ORDERBYsys_id &sysparm_record_count=3
1 | https://MYINSTANCE.service-now.com/incident.do?JSONv2&sysparm_action=getRecords&displayvalue=true&sysparm_view=mobile&sysparm_query=sys_id>1a0db534df120100cd7da5f59bf26304^ORDERBYsys_id&sysparm_record_count=3 |
This gets us the next set of three.
John, Thanks for writing this up! This is very helpful!
How would I do an insert?
I am trying to do a JSON insert using hurl.it, but I receive an error.
JSON Request
http://.service-now.com/cmn_location.do?JSONv2&sysparm_action=insert
Request Method: POST
Status Code: 200
Params: {
“name”: “Mike”,
“city”: “Minneapolis”,
“state”: “MN”
}
Response
{
“error”: “Request JSON object for insert cannot be null.”,
“reason”: null
}
What am I doing wrong?
@Mike…it almost sounds like the JSON string you created wasn’t included in the BODY of the post. Make sure that you are placing this in the body rather than in a Request Parameter.
You are are correct about the body of the post. I was using that hurl.it website and the “add body” button wasn’t so obvious.
Once I used “add body”. Worked great.
Thanks for the help.
Mike
John,
Is there any alternative around passing any parameter through rest URL?
I mean is there any way to use POST command for sending parameters(queries) in REST web services.
@Prateek, yes, the JSONv2 Web Service interface will accept a handful of URL parameters with the post. Refer to the wiki documentation for the details on those parameters. It will still require the POST body, however for the actual data to be set/updated on the table.
json login .please
https://rumbledev.service-now.com/incident.do?JSONv2?username=?&password=?
not work
How do you reset an existing value to null ?
Trying to sync from one SN instance to another.
The destination has a value but the source does not. Need to set the destination value to null to match the source.
Tried the expected “field_name”:””
Seems to be totally ignored since the “__status”:”success” response has the original target value still (“field_name”:”oldValue”).
Tried the alternate “field_name”:null
Seems to be at least recognized since the “__status”:”success” response shows a “field_name”:””, but the next query still returns the original target value.
I can’t seem to clear the value when the target has something there.
Hi John,
Is there a URL as well to Query the CI’s as well as URL to create a CI.
For example, I want to create a ComputerSystem and then query that ComputerSystem via REST
All examples shows querying/creating Incidents.
https://.service-now.com/incident.do?
was hoping there is a URL for CI as well like
https://.service-now.com/ci.do?
Thanks for your help.
Somehow my post from Jun 25th got lost 🙁
Let me make this short …
How do you nullify a field that has a value?
Neither of the two syntax versions work.
{
“field_name_1”:null,
“field_name_2″:””
}
Just trying to clear out a value from a field that has a value.
Can’t seem to get anything to work under Dublin release.
Figured out I was using the v1 interface (?JSON) which has the bug. the ?JSONv2 interface works as expected allowing :”” to nullify a sting field.
Could you tell me how to insert a new record for which one of the field is a reference field.
For example: I would like to insert the “caller ID” whose value is a reference value , but how to insert a record by specifying the display value of that reference field?
Please help
Hi @Deepak,
Did you ever figure out how to insert a record where some of the fields may be reference fields to another table? Trying to do this myself….
Thanks, -e
Hi, do you know how to attach a comment on an incident using REST API and JSONv2 ?