JSON array processing

Discuss RoboTask here
Post Reply
aseethar
Posts: 3
Joined: Wed Jan 11, 2023 6:55 am

JSON array processing

Post by aseethar »

I am trying to process a json array in a loop of a task
I have got the correct number of array elements and stuggling to retrieve each username
My json array looks like this - how do I get to the username so that I can set the value of a variable as I loop?
{
"usersToEndSessions": [
{
"Row ID": "fc5cb3f0-916d-11ed-a5df-01647197df94",
"Created": "2023-01-11T05:08:28.979Z",
"Updated": "2023-01-11T05:51:04.345Z",
"username": "john.steven"
},
{
"Row ID": "71b9bc90-915c-11ed-a4c8-ee77646f8fe3",
"Created": "2023-01-11T03:02:54.941Z",
"Updated": "2023-01-11T05:51:04.353Z",
"username": "sam.smith"
}
]
}

What I have tried so far:
Works: Set a variable local@usersToEndSessionsValue to {json:local@RESTRESPONSE.usersToEndSessions}
Does not work: Log Message {json:local@usersToEndSessionsValue[1].username}
I'd expect it to log a username but instead simply prints out the whole string
Could not find any robotask doc on json array processing in a loop - thanks for your help
Oleg
Site Admin
Posts: 3000
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: JSON array processing

Post by Oleg »

Works: Set a variable local@usersToEndSessionsValue to {json:local@RESTRESPONSE.usersToEndSessions}
Does not work: Log Message {json:local@usersToEndSessionsValue[1].username}
Why JSON? I think you need save your JSON to simple variable: local@RESTRESPONSE
Next you can process the value as JSON: {json:local@RESTRESPONSE.usersToEndSessionsValue[1].username}

Look at my example below. It works.
Save the text of the task to a file and use menu Task->Import to import the task into RoboTask.
Also you can simply copy task text and paste it into the task list.

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|693415217
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task29"
Hide=INTEGER|0
ID=INTEGER|-620743232
LogOnAsUser=INTEGER|1
Name=STRING|"Json array in the loop"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER
Action7=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set JSON to variable ""local@RESTRESPONSE"""
Params=FOLDER

[Actions\Action1\Params]
expand=STRING|"0"
linecount=STRING|"16"
varname=STRING|"local@RESTRESPONSE"
varvalue=STRING|"{"
varvalue00000001=STRING|"""usersToEndSessions"": ["
varvalue00000002=STRING|"{"
varvalue00000003=STRING|"""Row ID"": ""fc5cb3f0-916d-11ed-a5df-01647197df94"","
varvalue00000004=STRING|"""Created"": ""2023-01-11T05:08:28.979Z"","
varvalue00000005=STRING|"""Updated"": ""2023-01-11T05:51:04.345Z"","
varvalue00000006=STRING|"""username"": ""john.steven"""
varvalue00000007=STRING|"},"
varvalue00000008=STRING|"{"
varvalue00000009=STRING|"""Row ID"": ""71b9bc90-915c-11ed-a4c8-ee77646f8fe3"","
varvalue0000000A=STRING|"""Created"": ""2023-01-11T03:02:54.941Z"","
varvalue0000000B=STRING|"""Updated"": ""2023-01-11T05:51:04.353Z"","
varvalue0000000C=STRING|"""username"": ""sam.smith"""
varvalue0000000D=STRING|"}"
varvalue0000000E=STRING|"]"
varvalue0000000F=STRING|"}"

[Actions\Action2]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|0
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action2\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"{RestResponse}"
linecount=STRING|"1"
timeout=STRING|"10"

[Actions\Action3]
ActionID=STRING|"A_VARCOUNTELEMENTS"
Enabled=INTEGER|-1
Name=STRING|"Count array elements (as JSON array)"
Params=FOLDER

[Actions\Action3\Params]
mode=STRING|"1"
source=STRING|"{json:RestResponse.usersToEndSessions}"
variable=STRING|"num"

[Actions\Action4]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action4\Params]
message=STRING|"{num}"
type=STRING|"3"

[Actions\Action5]
ActionID=STRING|"A_LOOP_SIMPLE"
Enabled=INTEGER|-1
Name=STRING|"Simple Loop"
Params=FOLDER

[Actions\Action5\Params]
begin=STRING|"0"
end=STRING|"{Subtract({num},1)}"
step=STRING|"1"
variable=STRING|"ITEM"

[Actions\Action6]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action6\Params]
message=STRING|"{json:RestResponse.usersToEndSessions[{item}].username}"
type=STRING|"3"

[Actions\Action7]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

Oleg Yershov
aseethar
Posts: 3
Joined: Wed Jan 11, 2023 6:55 am

Re: JSON array processing

Post by aseethar »

Oleg, your solution was exactly what I was looking for. Using the new feature of Rest Client/Invoke Web Service and COUNTARRAY with JSON. Thanks!
Post Reply