JSON formatting to UiPath variable

I am a bit stuck on how to pass this with proper format to a UiPath variable and later pass it to http request for api body.Can some one help

JSON format looks like this

{
“Name”:[
Variable1
],
“ROLL”: variable2
}

Both variables are dynamic

Try below using Assign activity.
jsonBody = String.Format($"{{""Name"": [""{0}""], ""ROLL"": {1}}}",Variable1.ToString,Variable2.ToString)

@JITU99
we recommend to tell us also what expected Datatypes are variable1 and variable2 as it makes a difference to the resulting JSON

e.g. these are strings variables:

variable1 = “ABC”
variable1 = “RL100”

{
"Name":["ABC"],
"ROLL": "RL100"
}

e.g. these are int variables:

variable1 = 1000
variable1 = 456

{
"Name":[1000],
"ROLL": 456
}

@JITU99
Create a new JSON object,
Add the required object into it,
Then convert the JSON object into a string so it can be passed in your API call in the required format.


image
APITesting.xaml (9.3 KB)

I believe the screenshot below will help clarify your query.

HI @JITU99 ,

  • Variable1: Type String or List<String> depending on whether it’s a single value or an array.
  • Variable2: Type String, Int32, or another appropriate type.
jsonBody = "{
  ""Name"": [""" + Variable1 + """],
  ""ROLL"": """ + Variable2 + """
}"

If Variable2 is numeric, remove the quotes:

jsonBody = "{
  ""Name"": [""" + Variable1 + """],
  ""ROLL"": " + Variable2 + "
}"

In the HTTP Request Activity:

Try this and let us know your output.

Happy Automation!

@JITU99

first please check the formats that @ppr mentioned

then as you want to pass in http request you want to pass it as string..so enclose everything in double quotes and you need to escape all double quotes inside your json with extra double quotes

"{
""Name"":[" +
Variable1 +
"],
""ROLL"":" + variable2
+ "}"

cheers

Another fun way to do this is writing a template .json file and using Read Text File activity. That way, everything is properly formatted/escaped, and you can just use jsonBodyAsString.Replace(“Variable1Value”, Variable1)

Example, where Variable1 is “hello”:

{
"test" : "Variable1Value"
}

becomes

{
"test" : "hello"
}