
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 02:24 PM
I have an inbound scripted REST API I've developed for an integration with another system. This system will be sending transactional data to ServiceNow periodically.
The issue I'm having is that I want to capture the raw JSON from the REST message, as well as parse the data sent into variables which will be used to insert records into a table. I've found that I can't do both, at least not in the way which seems simplest.
var body = request.body;
var tDate = body.data.transactionDate;
As soon as I start pulling data out in the above fashion, the following returns an empty value.
gs.log(request.body.dataString);
If I try to use the dataString before the data object, I can't parse variables out in the manner shown in the first code block.
Does anyone have a (hopefully simple) solution to this requirement? To both save the JSON string sent through the API and to parse the data sent through the API into variables to be used to insert a table record.
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 03:12 PM
The request stream can only be read once. Dot-walking into request.body.data implicitly reads the body and parses it into an object. When you subsequently try to access the string, it's no longer available. And vice versa if you go for the dataString first.
Try something like this:
var requestString = request.body.dataString;
// Parse it for easy field extraction
var transaction = JSON.parse(requestString);
var tDate = transaction.transactionDate;
// Now do something else with the raw string
gs.debug(requestString);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 02:26 PM
Can't you just store it before you parse it?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 02:26 PM
it should be request.body.data.transactionDate.
you can add log statement to print value
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 03:12 PM
The request stream can only be read once. Dot-walking into request.body.data implicitly reads the body and parses it into an object. When you subsequently try to access the string, it's no longer available. And vice versa if you go for the dataString first.
Try something like this:
var requestString = request.body.dataString;
// Parse it for easy field extraction
var transaction = JSON.parse(requestString);
var tDate = transaction.transactionDate;
// Now do something else with the raw string
gs.debug(requestString);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2020 06:51 AM
Thanks Josh, that was the piece I wasn't understanding - I hadn't seen anything in the documentation about it only being able to be read once (although I'm sure I was looking in the wrong place).
Your solution works perfectly for our needs!