Azure Logic Apps .NET SDK json parsing error in workflow definition

278 views Asked by At

This is the code block that is failing when I call CreateOrUpdateAsync. The Logic App runs just fine in Azure, but when I try to create it using the .NET SDK I get JSON parsing issues.

'foreach': '@variables(""LocationData"")',
            'runAfter': {
                'Initialize_variable_2': [
                    'Succeeded'
                ]
            }

Here is the error message.

{"The template validation failed: 'The template action 'For_each' at line '1' and column '263' is not valid: \"The template language expression 'variables(\"LocationData\")' is not valid: the string character '\"' at position '10' is not expected.\".'."}

If I change to single quote, I get this error.

'foreach': '@variables('LocationData')',
                'runAfter': {
                    'Initialize_variable_2': [
                        'Succeeded'
                    ]
                }

{"After parsing a value an unexpected character was encountered: L. Path 'actions.For_each.foreach', line 651, position 40."}

This JSON formatter & validator doesn't like it either.

https://jsonformatter.curiousconcept.com/

Does anyone know the problem and how to fix it?

Any help is much appreciated! Thanks!

2

There are 2 answers

0
Dumber_Texan2 On BEST ANSWER

I went ahead and created my own Classes to handle the workflow definition.

Here is a sample. It was a lot of code, but it works great.

public class LogicApp
{
    public string schema { get; set; }
    public Actions actions { get; set; }
    public string contentVersion { get; set; }
    public Outputs outputs { get; set; }
    public Triggers triggers { get; set; }
} 

public class Actions
{
    public For_Each For_each { get; set; }
}

public class For_Each
{
    public Actions actions { get; set; }
    public string _foreach { get; set; }
    public Runafter runAfter { get; set; }
    public string type { get; set; }
}

I even wrote Classes to handle my ParseJson Actions. Here is a sample.

public class Parse_JSON
{
    public Inputs inputs { get; set; }
    public Runafter runAfter { get; set; }
    public string type { get; set; }
}

I then called it like so.

var logicApp = new LogicApp
{

}

Finally, I created the workflow definition JSON string. This is an ASP.Net Core app.

var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = ReplaceFirst(JsonSerializer.Serialize(logicApp, options), "schema", "$schema"));

I used this method to add a $ to the first occurrence of the word schema.

string ReplaceFirst(string text, string search, string replace)
{
       int pos = text.IndexOf(search);
       if (pos < 0)
       {
           return text;
       }
       return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
 }
1
SwethaKandikonda On

After reproducing from our end, here is how it was working for us.

"For_each": {
    "foreach": "@variables('location')",
    "runAfter": {
        "Initialize_variable_2": [
            "Succeeded"
        ]
    }
}