Importing data from a REST Web Service

thisismichaelb
Tera Contributor

I'm currently working on pulling our asset data out of MobileIron (mobile device management) and into ServiceNow. MobileIron does have an API, but the xml returned when you make a standard request (w/no headers) is garbage that ServiceNow can't read. It appears to use "{" where "<" should go....

Error Returned: org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]

You can submit an html request with a header of "Accept=application/xml" to get a nice clean xml file from MobileIron, which is what I have done through an outbound REST message to the MobileIron API.

If I could add a header into a data source (just like in the outbound RESET messages), my problem would be solved. Since I can't, I'm working on programmatically getting the data imported into our CMDB. I have good, solid code that gets the data I need and puts it into a table, but I'd still like to leverage System Import Sets to stage the data and then get it into the table it needs to be in.

Now the problem is, you have to have a data source to utilize a Scheduled Import Sets. Well, I don't have a valid data source for the Scheduled Import Set. I tried throwing my script into the "pre-script", but it ran and ran and ran and I ended up with WAY too many records in my CMDB.

There has to be a better way to do this. Is there something I'm missing?

5 REPLIES 5

john_andersen
Tera Guru

The reason the data source is not returning the information in the format you need is that the default API return format is JSON, not XML. From what you describe, it is possible to have it return XML if you send in an HTTP Header with your RESTful call. However, a data source is not configured to send HTTP Headers.

I would recommend you build a Scheduled Job Script that leverages the REST Message feature in ServiceNow. Then, when the response comes in JSON or XML, your script can handle the response by creating rows in an import set table that points to the CMDB.

Some good REST Message information:
- http://wiki.servicenow.com/index.php?title=REST_Message
- http://www.john-james-andersen.com/blog/service-now/servicenow-restmessage-coding-tips.html

Creating records from an XML response
- http://www.john-james-andersen.com/blog/service-now/converting-xml-to-a-record-in-servicenow.html


Hi, How to store the JSON response from REST message into the table?
I have been trying to do this, but couldnt get much info.
Any example/links would be of great help.


The easiest way to handle a JSON response is to convert it into an object first. Then you can do a GlideRecord operation on a table and save that data there.

Here is a blog post that explains how to parse the JSON into a Javascript object. Then you can use that object in conjunction with GlideRecord:

http://www.john-james-andersen.com/blog/service-now/converting-a-json-string-to-an-object-in-servicenow.html


Thanks much.. That's what i wanted