Passing InputArguments to Orchestrator API via JTokenWriter

Hi All,

I have read the various forum posts on starting Jobs on Orchestrator via the API and I can get it to work without any issues. I have since created an automation that takes in an input Argument. Looking at the various posts I can see that the arguments need to be passed in as a JSON string and appropriately escaped.

I Invoke the following Code via the ‘Invoke code’ method against a variable of type JTokenWriter:

BodyJSON.WriteStartObject()
BodyJSON.WritePropertyName(“startInfo”)
BodyJSON.WriteStartObject()
BodyJSON.WritePropertyName(“ReleaseKey”)
BodyJSON.WriteValue(ReleaseKey)
BodyJSON.WritePropertyName(“Strategy”)
BodyJSON.WriteValue(Strategy)
BodyJSON.WritePropertyName(“RobotIds”)
BodyJSON.WriteStartArray()
BodyJSON.WriteEndArray()
BodyJSON.WritePropertyName(“NoOfRobots”)
BodyJSON.WriteValue(RobotCount)
BodyJSON.WritePropertyName(“JobsCount”)
BodyJSON.WriteValue(0)
BodyJSON.WritePropertyName(“Source”)
BodyJSON.WriteValue(“Manual”)
BodyJSON.WritePropertyName(“InputArguments”)
BodyJSON.WriteStartObject()
BodyJSON.WritePropertyName(“Live_Test”)
BodyJSON.WriteValue(Mode)
BodyJSON.WriteEndObject()
BodyJSON.WriteEndObject()
BodyJSON.WriteEndObject()

If I remove the section:
BodyJSON.WritePropertyName(“InputArguments”)
BodyJSON.WriteStartObject()
BodyJSON.WritePropertyName(“Live_Test”)
BodyJSON.WriteValue(Mode)
BodyJSON.WriteEndObject()

Then it works fine so I know its the argument section of the code. What I can’t figure out is how to send the values as when I send them like this I get a 400 error code and the following response:

“{“message”:“An unexpected ‘StartObject’ node was found for property named ‘InputArguments’ when reading from the JSON reader. A ‘PrimitiveValue’ node was expected.”,“errorCode”:0,“resourceIds”:null}”

I am guessing that I somehow need to pass the values actually as a string rather? If so can anyone point in the right direction of how to do this as if I do the following:

BodyJSON.WritePropertyName(“InputArguments”)
BodyJSON.WriteValue("""{"“Live_Test”":"“Live”"}""")

Then I get the following output:
“{\r\n “startInfo”: {\r\n “ReleaseKey”: “0b0b7094-0ed5-4cf7-91f2-2ffbaaa744db”,\r\n “Strategy”: “RobotCount”,\r\n “RobotIds”: ,\r\n “NoOfRobots”: 1,\r\n “JobsCount”: 0,\r\n “Source”: “Manual”,\r\n “InputArguments”: “\\\”{\\\“Live_Test\\\”:\\\“Live\\\”}\\\”"\r\n }\r\n}"

There are then too many \\ characters

If you know the format of the string you want to send, isnt it simpler to just make a template string and just replace the values instead?

myStringTemplate = "{"startInfo":{"Release": "{0}"....}}"
myBody = String.Format(myStringTemplate, "0b0b7094-0ed5-4cf7-91f2-2ffbaaa744db", ...)

I am sorry but I do not understand how to implement that in the context of what I have already done. Is this done in the Invoke Code or as a different activity in UiPath?

This is plain string manipulation, you can even do with assign activities… using json objects for a simple, plain format body, seems cumbersome to me…

After doing a little research I understand what you are saying and I have implemented it and it is working. Thankyou

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.