I have to transform a json using Liquid template in Logic apps. The problem is the the Json attribute names (e.g. DynamicTag1, DynamicTag2) are not known when the file is received. The tag name could be anything which is not known before generating the file. How can I transform this Json file using Liquid?
Here is the input file:
{
"fields": {
"DynamicTag1": {
"type": "string",
"valueString": "SR12345678",
"text": "SR12345678",
"page": 1,
"confidence": 0.995
},
"DynamicTag2": {
"type": "string",
"valueString": "BR123456",
"text": "BR123456",
"page": 1,
"confidence": 0.995
},
"SomeOtherDynamicTag3": {
"type": "string",
"valueString": "QR567TY",
"text": "QR567TY",
"page": 1,
"confidence": 0.995
}
}
}
And here is the expected output:
{
"fields": [
{
"FieldName": "DynamicTag1",
"type": "string",
"valueString": "SR12345678",
"text": "SR12345678",
"page": 1,
"confidence": 0.995
},
{
"FieldName": "DynamicTag2",
"type": "string",
"valueString": "BR123456",
"text": "BR123456",
"page": 1,
"confidence": 0.995
},
{
"FieldName": "SomeOtherDynamicTag3",
"type": "string",
"valueString": "QR567TY",
"text": "QR567TY",
"page": 1,
"confidence": 0.995
}
]
}
CodePudding user response:
If you're happy to avoid the Liquid approach (not saying you should) then you could create an Azure Function that will take your JSON and transform it to what you need.
Create a new function called TransformJson and then refer to it in your Logic App. This is code ...
#r "Newtonsoft.Json"
using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = String.Empty;
using (StreamReader streamReader = new StreamReader(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
var jsonObject = JObject.Parse(requestBody);
var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder);
using (JsonWriter jsonWriter = new JsonTextWriter(stringWriter))
{
jsonWriter.Formatting = Newtonsoft.Json.Formatting.None;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("fields");
jsonWriter.WriteStartArray();
// Retrieve the first object and process each object below.
foreach (JObject subObject in jsonObject.First.Children())
{
foreach (JProperty dynamicObject in subObject.Children())
{
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("fieldName");
jsonWriter.WriteValue(dynamicObject.Name);
foreach (JProperty property in dynamicObject.First.Children())
{
jsonWriter.WritePropertyName(property.Name);
jsonWriter.WriteValue(property.Value);
}
jsonWriter.WriteEndObject();
}
}
jsonWriter.WriteEnd();
jsonWriter.WriteEndObject();
}
return new ContentResult()
{
Content = stringBuilder.ToString(),
ContentType = "application/json"
};
}
.. and then referring to it, I get the desired result.
Action
Result
It's an option.


