The template language function 'json' parameter is not valid | How to handle double quotes in JSON value

{tocify} $title={Table of Contents}

Error/Issue:


Found few instances of logic app failed today, in an action where there is call to rest api of an application with an error:


InvalidTemplate. Unable to process template language expressions in action 'HTTP_Call_to_xxx_Application' inputs at line '0' and column '0': 'The template language function 'json' parameter is not valid. The provided value '{ "payload": { "Environment":"dev", "number":"xxx", "Brand":"TestBrand1", "ChannelName":"channel_xxx", "Language":"en-US", "Status":"Failure", "siteURL":"http://xxx.dev-xxx.com", "InterfaceName":"xxx", "Reason":"xxx process Failed.xxxx publish failed : SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'field_includes_value' at row 1: INSERT INTO "node__field_includes" ("entity_id", "revision_id", "bundle", "delta", "langcode", "field_includes_value", "field_includes_format") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6);


The template language function 'json' parameter is not valid


Why it happened

In a Logic App, there is a HTTP action which is calling a rest API and content type is JSON. 

And as pointed by the error, JSON which was passed was not valid. 

Most of the run are successful, but few are failing with same error, JSON is not valid. 

So there was something wrong with the syntax, some deviation from rules of JSON Syntax.

The reason was presence of double quotes in value against Reason key.( highlighted in yellow in the above image)

Because of this 3 rules were breached (Highlighted in Yellow Below)

Below are few Rules for JSON syntax:

  • A JSON object is surrounded by curly braces {}.
  • The name-value pairs are grouped by a colon (:) and separated by a comma (,).
  • An array begins with a left bracket and ends with a right bracket [].
  • The trailing commas and leading zeros in a number are prohibited.
  • The octal and hexadecimal formats are not permitted.
  • Each key within the JSON should be unique and should be enclosed within the double-quotes.
  • If value is of type string then it should be enclosed within the double-quotes.
  • The boolean type matches only two special values: true and false and NULL values are represented by the null literal (without quotes).

What to do


So the area to be fixed here is handling double quotes in JSON value.

JSON requires double-quotes around string values and it also supports backslash-quoting(escaping) of certain special characters—including double quotes.

With the help of expression using replace function, before assigning the value to Reason key, replaced " with \".  

replace(body('Filter_array')?[0]?['error'],'"','\"')

So here adding \ (backslash) means telling the JSON parser that the next character is not part of the JSON string,  instead it is part of the data.





Post a Comment

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

Previous Post Next Post