Converting JsonString into String in UI Path Studio QA

I have a JsonString converted from Table Data like this
[{“column”:“Value”}, {“column1”: “value1”, {“column2”:“value2”}]

and I want to convert this JsonString to String as :
“[{\“column\”:\“value\”}, {\“column1\”:\“value1\”}, {\“column1\”:\“value1\”}]”
can some please help me on this?

Hi @ahmed.faisal

  • We use the Deserialize JSON activity to convert this JSON string into a generic object, storing the result in the variable deserializedObject.
  • We then use the .ToString() method to convert this generic object to a string and store it in the variable outputString.

Thanks,
Lakshmi

I am getting this error : Newtonsoft.Json.JsonSerializationException: Deserialized JSON type ‘Newtonsoft.Json.Linq.JArray’ is not compatible with expected type ‘Newtonsoft.Json.Linq.JObject’. Path ‘’, line 1, position 2273. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJToken(JsonReader reader, JsonContract contract)

Change the Data Type to Newtonsoft.Json.Linq.JObject

Can you share the json you are trying to deseriaize?

This is actually a client Data, So I really can’t share that, but that’s the format I have.

I extracted the tableData and directly converted to JsonString by using this formula “Newtonsoft.Json.JsonConvert.SerializeObject(tableData)”, but this format is not able to send it to postman as JsonString, it requires as the second format.

I think the json is not valid

It should something like this
{
“Columns”: [
{
“column”: “Value”
},
{
“column1”: “Value1”
}
]
}

Are you getting error in the deserialize json activity?

No, the format is correct basically from the tableData I am converting into JsonString, So it is basically a JsonArray but in String format.

Yes While Deserializing I am getting the error, but for Deserialize JsonArray and store it as JObject I wont get any errorbut Once Deserialized and print output using log message as JObject.ToString, I am still getting the Same output but as JsonArray Format. but I want second format.

When you take the first format and paste it on Json Stringify website, it will give second format

Use the Deserialize JSON Array activity to parse the JSON array into a .NET object.
Use the Serialize method to convert into a JSON string.
Newtonsoft.Json.JsonConvert.SerializeObject(jsonArrayObject)

Can you try this

Hi @ahmed.faisal ,

Could you maybe check with the below Expression :

System.Web.HttpUtility.JavaScriptStringEncode(Info)

Here, Info variable is the Json String data.

Debug Panel :
image

For better visual of the required output, write the Expression to a text file. We should get the output in the below manner :
image

image

1 Like

@ahmed.faisal try using Deserialize Json Array instead :v: !!

Using this “System.Web.HttpUtility.JavaScriptStringEncode(Info)”, it is working thanks for the help for all of you here

1 Like