How Do I Properly Receive REST API Response

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
bssxfire8
Posts: 46
Joined: Fri Oct 02, 2015 11:41 pm

How Do I Properly Receive REST API Response

Post by bssxfire8 »

I am trying to get a Rest API process working for tsheets pulling jobcodes.
My main question is How do I handle a response value that contains a table of values but is also variable itself and not just a value label/name?
For example Like I have colored red in my response code below:
{
"results": {
"jobcodes": {
"21358474": {
"id": 21358474,
"parent_id": 0, ........

I assume that is what is holding up the filling out my BOs and Attributes with all of my response data.

Here is the JSON response:

Code: Select all

{
 "results": {
  "jobcodes": {
   [color=#FF0000]"21358474": {[/color]
    "id": 21358474,
    "parent_id": 0,
    "assigned_to_all": false,
    "billable": false,
    "active": true,
    "type": "regular",
    "has_children": false,
    "billable_rate": 0,
    "short_code": "",
    "name": "1 CASH CUSTOMER ACCOUNT",
    "last_modified": "2020-01-24T20:00:39+00:00",
    "created": "2019-10-28T22:56:24+00:00",
    "project_id": 0,
    "filtered_customfielditems": "",
    "required_customfields": [
     
    ],
    "locations": [
     
    ],
    "geofence_config_id": 0
   },
   "5868600": {
    "id": 5868600,
    "parent_id": 0,
    "assigned_to_all": true,
    "billable": false,
    "active": true,
    "type": "regular",
    "has_children": false,
    "billable_rate": 0,
    "short_code": "",
    "name": "100",
    "last_modified": "2020-02-28T21:47:03+00:00",
    "created": "2017-07-12T20:05:02+00:00",
    "project_id": 0,
    "filtered_customfielditems": "",
    "required_customfields": [
     
    ],
    "locations": [
     5181629
    ],
    "geofence_config_id": 0
   },
   "19845277": {
    "id": 19845277,
    "parent_id": 0,
    "assigned_to_all": false,
    "billable": false,
    "active": true,
    "type": "regular",
    "has_children": false,
    "billable_rate": 0,
    "short_code": "",
    "name": "101-Admin",
    "last_modified": "2019-06-03T22:15:31+00:00",
    "created": "2019-04-15T23:21:09+00:00",
    "project_id": 0,
    "filtered_customfielditems": "",
    "required_customfields": [
     
    ],
    "locations": [
     
    ],
    "geofence_config_id": 0
   }
  }
 },
 "more": true,
 "supplemental_data": {
  "locations": {
   "5181629": {
    "id": 5181629,
    "addr1": "100 Commercial Ln",
    "addr2": "",
    "city": "Pineville",
    "state": "MO",
    "zip": "64856",
    "formatted_address": "100 Commercial Ln, Pineville, MO 64856",
    "country": "US",
    "active": true,
    "latitude": 36.507632600000001,
    "longitude": -94.278914499999999,
    "place_id": "",
    "place_id_hash": "53c49817e3cd542e82bbca58ff8eff7d",
    "label": "100",
    "notes": "",
    "geocoding_status": "complete",
    "created": "2019-04-15T23:21:09+00:00",
    "last_modified": "2019-04-15T23:21:12+00:00",
    "geofence_config_id": null,
    "linked_objects": {
     "jobcodes": [
      5868600
     ]
    }
   }
  }
 }
}
My Business object structure looks like this:
Image

I only get data in these objects/attributes:
TSheetsResponse
TSheetsResponse.results (Reference Object)
TSheetsResponse.more (true/false attribute)
TSheetsResponse.supplemental_data (Reference Object)
bssxfire8
Posts: 46
Joined: Fri Oct 02, 2015 11:41 pm

Re: How Do I Properly Receive REST API Response

Post by bssxfire8 »

I was able to get a solution, I have had to research this and if this is a ridiculously easy thing to do that everyone already knows then I will take down this post but I think that if it is useful to some then awesome!

Because I think my first post was unclear, what I am trying to do is identify and use the data stored under a dynamic key that is in a numeric format in a JSON API response.
I have no concept of whether this is a difficult thing to do normally as this is not even close to my area of expertise.

Sorry for the long intro. Here's my solution:
I got my API Response into a plain text attribute of my TSheetsResponse Object.
I then have to run this javascript to parse through and create my job records:

Code: Select all

AwareApp.getObjectData (
  context[0].objectName,
  context[0].objectId,
  function (objectData)
  {
  var txt =objectData["ResponseText"];
  var obj = JSON.parse(txt);
  var results = obj.results;
  var jobcodes = results.jobcodes;
  
  for (var key of Object.keys(jobcodes)) {
    var jobcode=jobcodes[key];
    AwareApp.createOrUpdateObject ("Job",null,{ job_ID: jobcode["id"], name: jobcode["name"],parent_id: jobcode["parent_id"]},)
  }}
  );

In Short it works.
The Job Object populates with the data I want.
Jaymer
Posts: 2430
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Re: How Do I Properly Receive REST API Response

Post by Jaymer »

Brilliant!
I know I looked into this when you asked. And knew it couldn't be done by Aware in normal fashion. Glad you solved this.

Qs:
So you do a REQUEST SERVICE....
And then a BO "ResponseText" record is created with the plain text response in 1 field.
So how do you process that record with JS?
I've done JS in a Form, but have never applied JS to just data from a record? How?

Can you please explain (or show screen caps) of the Process/Rules/etc ?
thx
Click Here to see a collection of my tips & hacks on this forum. Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
bssxfire8
Posts: 46
Joined: Fri Oct 02, 2015 11:41 pm

Re: How Do I Properly Receive REST API Response

Post by bssxfire8 »

I think screen shots with some descriptions will best explain my thinking here.
1.Simple REST Service setup with the reply going into the plain text attribute - ResponseText of my TSheetsResponse BO

Image

2.Here's the BO... Nothing special No rules.

Image

3.This is the REQUEST SERVICE. I have a few extra rules that are simple enough.
  • Clean Up old Data - a process that runs the CLEANs Place Holder BOs (mostly just for diagnostics through this).
  • Create TSheets Response - Creates the TSheetsResponse Object and set the Parameters attribute that it holds.
  • Check for Object - Check if my TSheets REST object exists, If not, make it exist.
  • Call Service - Plain Jane REQUEST SERVICE for my TSheets Service.
  • Parse Jobs - This is my script to parse through my Reply.
    • The Trickiest/dumbest part was dealing with the double quote, single quote garbage.
    • I used Aware's AwareApp.getObjectData function where I can give it my TSheetsResponse ID which is in context and allows me to return any attribute I want from it.
    • I am able to solve my original problem by this beauty right here: Object.Keys(jobcodes) which returns the immediate child keys of the jobcodes key.
    • I return the Specific values I need for my TSheetsJob object Inside the AwareApp.createOrUpdateObject to create the objects I need.
  • Execute SP - Runs a stored procedure I have out there to do some stuff with the object that I wanted to do in sql instead of Aware.
Image
Jaymer
Posts: 2430
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Super Brilliant!

Post by Jaymer »

clap.jpg
clap.jpg (37.03 KiB) Viewed 8828 times
.
Click Here to see a collection of my tips & hacks on this forum. Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
Post Reply