Reputation: 387
I've read and followed multiple posts but I can't fix my problem:
I've created a APIM(Azure API Management) service and this works, the gateway url www.azurebroker.nl/azurebroker/factuur(for example) that does a request to my own API(www.ownapi.nl/invoice). The response of this API is as following:
{
"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]
}
Now I'm trying to transform the structure of the response above to a different structure, for example:
{
"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 ]
}
As you look closely, some fields are changed, for example:
Firstname + Lastname is now Name. invoiceID, formType, amount, currency, description are now put in a object named "general".
I've tried doing the following in Logic Apps:
The request body schema input field of this action is a schema of the first JSON mentioned in this article. After this action I've tried the action "Parse JSON":
In the Schema input field of the action "Parse Json" I've filled a JSON schema of the second JSON mentioned in this article.
Hope my goal is clear to you guys and someone can help me out. I'm trying to map the json structure of the response of my request I'm making with my API Management Gateway URL.
Thanks in advance
Upvotes: 0
Views: 812
Reputation: 7795
If you want to use LA, you could use send-request
policy in APIM to compose new HTTP request and make a call to LA, and then use set-body
to override response body with response from LA. But if this JSON transformation is all you need, you could avoid using LA and do everything in APIM. Add this inside outbound
section of your operation policy:
<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>
Depending on your exact transformations you may perfer to cherry-pick all properties instead, or even use liquid template to construct body, set-body
policy supports that.
Upvotes: 1