The template language function 'replace' expects its first parameter 'string' to be a string. The provided value is of type 'Object'

{tocify} $title={Table of Contents}


Issue


Was working on a requirement to handle quotes(") coming in the value of a property/field, for that made use of replace() function to add escape character.

Read following for detail --  How to handle double quotes in JSON value 

However, while doing so - received following error at runtime



The template language function 'replace' expects its first parameter 'string' to be a string. The provided value is of type 'Object'




InvalidTemplate. Unable to process template language expressions in action 'Log_Success' inputs at line '0' and column '0': 'The template language function 'replace' expects its first parameter 'string' to be a string. The provided value is of type 'Object'. Please see https://aka.ms/logicexpressions#replace for usage details.'.

Why it happened


replace() is a part of string functions available in Logic app, which can be used in expressions.

It basically replaces a substring within the specified string, and returns the updated string.

I was applying replace function on the output of an action which was calling an http endpoint

replace(replace(replace(body('Delete_API'),'}',''),'{',''),'"','\"') 

So, there was error due to the type of Delete_API (http endpoint) i.e. the output of Delete_API was a Object and not string.



What to do


To make replace function complain free, it should be provided with a string as input.

So how to convert the object to String?

We can make use of string() function.

string() is part of conversion function available in Logic app and can be used in expression and it returns the string version for an input value.

replace(replace(replace(string(body('Delete_API')),'}',''),'{',''),'"','\"')

So, provided the output of Delete_API to string function, which later was provided as input to replace() function.




If you have questions or suggestions, feel free to do in comments section below !!!


Do share if you find this helpful .......
 
                          Knowledge Sharing is Caring !!!!!!




Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post