Jesse
November 27, 2020, 1:27am
1
I have this JSON and I want to extract Role from it.
{
"status": "success",
"data": {
"shift": {
"id": 6410270
},
"scheduled": true,
"role": {
"id": 750,
"name": "Barback"
}
},
"message": ""
}
Here is my script however it is saying its not a valid object? Forgive me this should be simple json but I am having issues. If anyone can guide me I would appreciate it.
function getscheduledshifts(userpin) {
var userid = getuser(userpin);
var getshift = web.PostJson('https://api.7shifts.com/v1/'+method5+'/','{ "punch_id": '+userid+'}',''+apikey+'','');
var jsondata = JSON.parse(getshift);
var role = jsondata.data.role.id;
return role;
}
Here is what I get:
My question is… today there would be no value because there is no shift for today. Would that be why its reporting null? Or should it report empty? I would have assumed it would just repot empty but maybe not.
Jesse
November 27, 2020, 2:20am
2
Nevermind I had wrong user for it.
1 Like
Memo
November 27, 2020, 2:44am
3
The most frustrating thing I’ve come to terms with over the last 6 moths is that the code does exactly what it was told.
It still doesn’t stop me from wanting to launch my computer out the window.
And then there are times everything works and I have no earthly idea why.
Why not add null or empty checks just to be on the safe side. Maybe log it to the samba log file, too.
2 Likes
Jesse
November 27, 2020, 2:49am
4
actually i was wrong I had it right. for some reason its not storing the data… I think its on 7shifts side.
Jesse
November 27, 2020, 2:49am
5
As you see the object worked but it contains only a small amount of the data shown in their api doc
Posflow
November 27, 2020, 2:58am
6
Not sure why but when I was doing integration last time, I was forced to use "
instead of '
. If I used '
it was returning all sort of wild cards.
Also, cant you send whole string values as a variable?
Jesse
November 27, 2020, 3:04am
7
That was not the actual json i was getting is the problem
Memo
November 27, 2020, 3:05am
8
Does it work with another userid?
Jesse
November 27, 2020, 3:09am
9
Returns the same data on all userid’s
Jesse
November 27, 2020, 3:09am
10
It looks like this part of their API is not working
Jesse
November 27, 2020, 3:09am
11
Or they changed it and didnt document the change.
Jesse
November 27, 2020, 3:11am
12
The api call is working its returning success. Its just not returning the full object. So that tells me something is not working on their api.
Posflow
November 27, 2020, 3:14am
13
Do you have to request additional object through different end point or something?
Memo
November 27, 2020, 3:14am
14
I take it method5
is declared somewhere as shifts/scheduled
I just noticed, try removing the trailing ‘/’ from the endpoint.
Try this:
var getshift = web.PostJson('https://api.7shifts.com/v1/'+method5,'{ "punch_id": '+userid+'}',''+apikey+'','');
or maybe
var getshift = web.PostJson('https://api.7shifts.com/v1/'+method5+'','{ "punch_id": '+userid+'}',''+apikey+'','');
I’m not sure if js looks for an end of string after concat
Memo
November 27, 2020, 3:25am
15
There may be too many single quotes around api key
var getshift = web.PostJson('https://api.7shifts.com/v1/'+method5,'{ "punch_id": '+userid+'}',apikey,'');
Just the api key variable should work as the compiler should recognise it as a string.
I don’t know if MS’s implementation of JS handles string interpolation, but maybe this will work:
var getshift = web.PostJson('https://api.7shifts.com/v1/${method5}','{{ "punch_id": ${userid}}}',apikey,'');
EDIT:
never mind, string interpolation doesn’t work
Jesse
November 27, 2020, 3:42am
16
The json post is correct. I get success returned so it is correct. Yes Method5 is shifts/scheduled
Memo
November 27, 2020, 2:00pm
17
It may also be possible that the designers of the API chose not to return 404 in the event of no record(s) found.
200 could mean the request was properly formed and authorised, the request executed without any internal errors but returned no records. It’s is sometimes up to the receiver to parse the response to see if there’s an error and/or requested data was returned.
Jesse
November 27, 2020, 2:07pm
18
Yeah I scheduled a shift for that user as it’s a test user. It returned same data. I’ll reach out to 7shifts today