使用动态标签的液体模板转换 json
Transform json using liquid template for dynamic tags
我必须在 Logic 应用程序中使用 Liquid 模板转换 json。问题是 Json 属性名称(例如 DynamicTag1、DynamicTag2)在收到文件时未知。标签名称可以是生成文件之前未知的任何名称。如何使用 Liquid 转换此 Json 文件?
这是输入文件:
{
"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
}
}
}
这是预期的输出:
{
"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
}
]
}
如果您乐于避免使用 Liquid 方法(不是说您应该),那么您可以创建一个 Azure Functions 来获取您的 JSON 并将其转换为您需要的内容。
创建一个名为 TransformJson
的新函数,然后在您的逻辑应用程序中引用它。这是代码...
#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"
};
}
..再参考一下,就得到了想要的结果
动作
结果
这是一个选项。
我必须在 Logic 应用程序中使用 Liquid 模板转换 json。问题是 Json 属性名称(例如 DynamicTag1、DynamicTag2)在收到文件时未知。标签名称可以是生成文件之前未知的任何名称。如何使用 Liquid 转换此 Json 文件?
这是输入文件:
{
"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
}
}
}
这是预期的输出:
{
"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
}
]
}
如果您乐于避免使用 Liquid 方法(不是说您应该),那么您可以创建一个 Azure Functions 来获取您的 JSON 并将其转换为您需要的内容。
创建一个名为 TransformJson
的新函数,然后在您的逻辑应用程序中引用它。这是代码...
#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"
};
}
..再参考一下,就得到了想要的结果
动作
结果
这是一个选项。