如何将 Azure APIM 连接到逻辑应用程序以转换 API 响应的 JSON 结构?

How to connect Azure APIM to Logic Apps to transform JSON structure of API response?

我已经阅读并关注了多个帖子,但我无法解决我的问题:

我已经创建了一个 APIM(Azure API 管理) 服务并且这有效,网关 url www.azurebroker.nl/azurebroker/factuur(例如)向我自己的 API(www.ownapi.nl/invoice) 发出请求。这个API的响应如下:

{
"invoiceID":1,
"formType":"invoice",
"amount":449,
"currency":"eur",
"description":"Invoice real estate",
"period":{"end":20122019,"start":20122020},
"owner"{"id":91434,"firstname":"User","lastname":"UserName","dateOfBirth":1121993,"phoneNumber":3487378434,"countryOfBirth":"Nederland","IBAN":"NL28 ABNA 743734763474324"},
"property":{"id":105,"type":"apartment","address":"ghost lane 13","ZIP":"7888 CK","State\/Province":"Groningen","country":"Nederland","construction-year":15072009,"previousOwners":9},
"previousProperties":[54,193,11,454,18]
}

现在我正在尝试将上面的响应结构转换为不同的结构,例如:

{
"general": {
"invoiceID": 12,
"formType": "invoice",
"amount": 449,
"currency": "eur",
"description": "Invoice real estate",
"period": {
  "end": 20122019,
  "start": 20122020
}
},
"owner": {
"id": 91434,
"name": "User, Username",
"dateOfBirth": 1121993,
"phoneNumber": 646068151,
"countryOfBirth": "Nederland",
"IBAN": "NL28 ABNA 743734763474324"
},
"property": {
  "id": 105,
  "type": "apartment",
  "fullAddress": "ghost lane 13, 7888 CK Groningen Nederland",
  "construction-year": 15072009,
  "previousOwners": 9
},
"previousProperties": [ 54, 193, 11, 454, 18 ]
}

仔细观察,有些字段发生了变化,例如:

名字 + 姓氏现在是姓名。 invoiceID、formType、amount、currency、description 现在放在一个名为 "general".

的对象中

我尝试在逻辑应用程序中执行以下操作:

该动作的请求体架构输入字段是本文提到的第一个JSON的架构。完成此操作后,我尝试了 "Parse JSON":

在操作的架构输入字段中 "Parse Json" 我填写了本文中提到的第二个 JSON 的 JSON 架构。

希望你们清楚我的目标并且有人可以帮助我。我正在尝试映射我使用 API 管理网关 URL.

发出的请求响应的 json 结构

提前致谢

如果你想使用 LA,你可以在 APIM 中使用 send-request 策略来组合新的 HTTP 请求并调用 LA,然后使用 set-body 来覆盖响应主体洛杉矶。但是如果这个 JSON 转换是你所需要的,你可以避免使用 LA 并在 APIM 中做所有事情。在您的操作策略的 outbound 部分中添加:

<set-body>@{
    var body = context.Response.Body.As<JObject>();

    var newBody = new JObject(new JProperty("general", body));

    var owner = (JObject)body["owner"];
    owner["name"] = $"{owner["firstname"]}, {owner["lastname"]}";
    owner.Remove("firstname");
    owner.Remove("lastname");
    body.Remove("owner");
    newBody.Add("owner", owner);

    var property = (JObject)body["property"];
    property["fullAddress"] = $"{property["address"]}, {property["ZIP"]} {property["State/Province"]} {property["country"]}";
    property.Remove("address");
    property.Remove("ZIP");
    property.Remove("State/Province");
    property.Remove("country");
    body.Remove("property");
    newBody.Add("property", property);

    var previousProperties = (JArray)body["previousProperties"];
    body.Remove("previousProperties");
    newBody.Add("previousProperties", previousProperties);

    return newBody.ToString();
}</set-body>

根据您的确切转换,您可能更喜欢 cherry-pick 所有属性,甚至使用液体模板构建主体,set-body 政策 supports that