How to Access propertys from data, Toolbox server script

Hey @mishav

I been struggeling for a bit know and im trying to figure out how i should set up the server script in backend.

I am sending in data in the form of a JSON object, lets say the property name is “myProperty”.
When I try to access this property by reading data.myProperty I get “undefined”.
I have tried to do
const indata = JSON.parse(data)
and then try to read indata.myProperty, but even this returns “undefined”.
How do I access the JSON object and its properties inside the Server script event?



JSON
“status” : “Not Started”,
“shiftID”:“50945649”,
“userId”:“1”,
“title”:“Waiter”,
“salaryCode”:“610”,
“team”:"Waiter ",
“userName”:"Pontus S. Lindroth ",
“accountantId”:“5”,
“baseSalary”:200,
“start”:1705044190811,
“end”:1705076590828,
“shiftStatus”:1,
“weekHours”:2218800,
“montHours”:4310400,
“employeeType”:“5”,

“records”:,

"supplements":[{
     "id":"1",
     "title":"Kvelds tillegg",
     "salaryCode":"1",
     "type":"From Hour to Hour",
     "wageAdjustment":"kr (amount is added to exicting rate)",
     "pcsValue":9,
     "start":1677700800000,
     "end":1679455800000,
     "afterMinutes":"",
     "duration":"",
     "weekDays":"1, 2, 3, 4, 5",
     "range":"",

“validFrom” : 1680300000000,
“vaildUntil” : 4107614400000,
“employeeTypes”:“”,
“teams” : “Waiter, Chef”

Hi @sxtn.digital

FYI The editor for forum posts has a button that looks like </> which tells it that you are typing code.

A JSON object in text should look like:

{
  "myProperty": "my value",
  "colour": "blue",
  "numberOfDoors": 4,
  "myList": [
      "Text item 1",
      "Text item 2"
  ]
}

JSON-safe is intended to work for individual field values.

From the screenshot, you are taking some text and applying :formatted as JSON-safe.

What this does is take your JSON string, and make the entire thing a safe text value, ie. escaping all the characters that give it structure. this is bad, as you want to use the structure.

You can see this if you write the same expression to your logging table.

Yes this is expected, because data is not an object with properties, it is plain text.

This step looks fine, if data is given the appropriate JSON.

This should work if the JSON structure is correct in the data string.

Your screenshot shows for part of the output:

`Alex ${data.baseSalary}, ...`

This is not going to work, you need to be referencing the JS object not the JSON text. For example:

`Alex ${indata.baseSalary}, ...`

You also have ${indata.length} which only works if the JSON represents a list, but it doesn’t, it represents a single object.

Object.entries(data) won’t work, because data is a string.
Object.entries(indata).toString() should work fine, although I don’t know why you are taking a list, converting to string, then returning it as a list item.

Summary

It is great that you have showed your attempts, it shows you understand it, and there are only minor issues.

I could likely help you more if you describe what you want to achieve.

Kind regards,
Misha

2 Likes

Thanks Mishav! this is great, i will give it one more shot to nigth. I will update you on the outcome.

Hey @mishav

So im trying to run a calculation script. But it seems to be hard for us to figure out the syntax.

I load the salary data thru the JSON as i showned above, then regaring to supplements, date restriction the JS i giving me back a “calculcated salary” as a intiger. And a list of text where i pass all supplements addons.

As i metion, its working like a charm in the frontend with the javascript element. its just the backend script we cant figure out.

JSON

{
“status”: “Not Started”,
“shiftID”: “31692936”,
“userId”: “73465444”,
“title”: “Teamleder”,
“salaryCode”: “”,
“team”: "Teamleder ",
“userName”: "Pontus S. Lindroth ",
“accountantId”: “”,
“baseSalary”: 200,
“start”: 1705909917425,
“end”: 1705942317438,
“shiftStatus”: 1,
“weekHours”: 0,
“montHours”: 0,
“employeeType”: “”,

“records”: [

],

"supplements": [

{
“id”:" 1",
“title”:" Overtid 40%“,
“salaryCode”:” 1",
“type”:" Day - After x Hours",
“wageAdjustment”:" Percentage",
“pcsValue”: 40,
“start”: 1705878000000,
“end”: 1705878000000,
“afterMinutes”: “”,
“duration”: “”,
“weekDays”: “1, 2, 3, 4, 5, 6, 0”,
“range”: “week”,
“validFrom”: 1705791600000,
“vaildUntil”: 3630956400000,
“employeeTypes”: “1”,
“teams”: “Servitør, Servitør, Teamleder”
}]
}

Let say i whant to use this JSON set as data (whitout “format as JSON”) and do following and want to get following result.

output1 = baseSalary * 3
outputlist1 = [“team”, “userName”, “userId”]

What would be the code for that?