How to pass dynamic variable to json string

Hi all.
I want to pass variables dynamically into JSON string,but i’m getting an error saying input string was not in correct format though all variables are in type string. Following is the JSON string,

“Ref”: [
{
“Entity”: {
“type”: “{0}”,
“value”: “{1}”
}
}
]
Someone please suggest how to pass values.
Thanks in advance.

3 Likes

Hello @Shree_Vybhavi
try something like
JsonString = JsonString.Replace("{0}",YourVariable0).Replace("{1}",YourVariable1)

2 Likes

try once like this String.Format(Jsonstring_data,value1,value2)

Fine if this is in a string format like this string is saved in a text file (.txt)
–then we can use read text file activity and get the output with a variable of type string
named str_text
–now use a assign activity like this
str_text = String.Format(str_text,“yourvalue1”,“yourvalue2”)

so these value1 and value2 will get replaced in the place holder {0},{1},{2},…

Cheers @Shree_Vybhavi

Thank you @AkshaySandhu it worked…:slight_smile:

I tried using string.format but it didn’t work:(

yes @Shree_Vybhavi
String.Format wont work because this method look for instances of curly braces with digit/s like {0},{1} etc to replace it with desired value…
As in our case we are using Json String, it is failing…

There is still way to do it with String.format… but it is not feasible

all you have to do is escape curly braces by doubling it like below

“Ref”: [ {{ “Entity”: {{ “type”: “{0}”, “value”: “{1}” }} }} ]

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