How to send json body to Http request activity in studio

Hi, I am a newbie to UiPath . I want to send json body to Https request.
I have tried few ways but nothing seems to be working.

“{‘sk’: '”+row(“sk”).ToString+"’,‘form’: ‘"+row(“form”).ToString+"’,‘category1’: ‘"+row(“category1”).ToString+"’, ‘mandatedDays’: ‘"+row(“mandatedDays”).ToString+"’,‘category2’: ‘"+row(“category2”).ToString+"’,‘country’: ‘"+row(“country”).ToString+"’,‘name’:’"+row(“name”).ToString+"’}"

Studio doesn’t seem to give any error for the above format but the api endpoint that I am calling is not accepting the key value pairs with single quotes.

It would be great if I could get an example on this
Thanks

@Supreetha_Achar Can you try something in this way and check:

image

1 Like

Hi @Supreetha_Achar welcome to forum

Check this way of adding the json data

hope it helps

regards

Nived N

The character escaping is very important when passing the JSON as showed by @supermanPunch, in the example.

This solution works absolutely fine when the value is not dynamic.
I am trying to get the value from a csv file as row(“row1”).ToString
That’s where I am facing the issue

@Supreetha_Achar Can you try using the variable in this way :
image

You could try and create the json as a jobject then in the body field of the Http activity add the jobject as

jobject.tostring.

This works for me and then you can update the fields of the jobject dynamically. To create the jobject you can use the deserialize activity on a string variable. Once jobject is created you can update properties with an assign activity

jobject(“property”) = NewValue

To avoid the confusion of the many escape chars, you could create a separeted template of your json with some placeholder or string format and replace them on execution time.

  1. Create a file, can be a txt or json file:

{“key”:"{0}"}

The {0} indicates that would the place that will be replaced when using String.Format

  1. Use read txt file activity, then you’ll have a string var.

  2. Replace the variables:

String.Format(stringGeneratedFromReadFileActivity, {“value”})

Now you have: {“key”:“value”}.

  1. Convert it to JObject using: Newtonsoft.Json.Linq.JObject.Parse(stringGeneratedFromReadFileActivity)

  2. Try to pass your jobject as string: newJObjectVar.ToString

Maybe not the most performatic way to do it, but it could help you to avoid a headache with escape chars.