Little JSON Help

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:

image

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.

Nevermind I had wrong user for it.

1 Like

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

actually i was wrong I had it right. for some reason its not storing the data… I think its on 7shifts side.

image

As you see the object worked but it contains only a small amount of the data shown in their api doc

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?

That was not the actual json i was getting is the problem

Does it work with another userid?

Returns the same data on all userid’s

It looks like this part of their API is not working

Or they changed it and didnt document the change.

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.

Do you have to request additional object through different end point or something?

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

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

The json post is correct. I get success returned so it is correct. Yes Method5 is shifts/scheduled

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.

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