Hi experts,
Is there any best practice to receive and process a complex JSON Payload via http, without the need of it mapping to a BC Page/Query?
Example:
{
"title": "mibuso",
"users": [
{
"name": "Wisa123",
"property1": "one",
"property2": "two",
"posts": [
{
"title":"OData: receiving JSON not mapping to BC Pages/Queries",
"answers":10,
"solved":true
}
]
},
{
"name": "test",
"property1": "one",
"property2": "two",
"posts": [
{
"title":"testing stuff",
"answers":10,
"solved":true
}
]
},
...
]
}
This is just some data I made up for the sake of the thread here. I need to create an endpoint that receives this document and for example creates Employees based on the usernames.
If API Pages could just have something like this things would be easy:
local procedure Post(payload: JsonObject)
But since everything that is sent towards BC needs to comply with the format the Page/Query define things are tricky. Especially when complex Types and Arrays come into play. I've looked into EdmTypes and kinda managed to get something to work.
Sadly that integration ("Odata Edm Type") is undocumented, also there are barely any examples out there (vjeko did something on this, but not in the complexity I'm asking for).
Any help appreciated, I realize this is a very special request, and most people won't stumble across this in normal projects.
Answers
Take a look to this post, I think that it could give you some ideas:
https://www.kauffmann.nl/2020/03/05/codeunit-apis-in-business-central/
Regards
Thanks for that link @ftornero I actually didn't stumple upon that one while googling.
However, even AJK mentions
Well the OdataEdmTypes are exactly this possibility to create custom entities.
However these OdataEdmTypes seem to be limited to relatively simple/shallow JSON documents.
By now i spent a lot (way too much) time trying to get the EdmTypes to work. I also created a GIthub issue regarding this.
https://github.com/microsoft/ALAppExtensions/issues/6360
I'll try to keep this thread updated in case something happens in this issue, so future people (and probably myself in a few years) can find this.
Regards
I got really frustrated when I found out how limited this is and spent some time trying to work around this without success. Since then I have accepted that I'm limited to three levels, API Page/SubPage/Complex Type and sometimes I have also flattened the data to work with this limitation. It's nice move to create Github issue, hopefully it returns something.
Using how base the AJK code you can create this procedure in the codeunit to test the data back and forth of BC:
And with "REST Client" inside VSCode you can call that procedure:
And you must get back the same JSON data you send.
The backslashs are necesary for the "REST Client", I have an example with Powershell where you deal with the JSON data in a better way.
Bottom line is you pass to BC a text parameter (RequestValue) with the JSON data and you can process it in BC and do whatever you need to do.
Regards
Thanks for the example and explanation.
This obviously works, but there is still a problem.
In order to work like this you need to have control over the sent/received JSON structure.
In my example the JSON I described in the OP is the HTTP Payload, and there is no altering it since it comes from an external system.
Since I can't tell the external system that POST's towards my BC to wrap everything up in a single JSON Object this won't work out like this. Still looking for a solution to this. This is what an incoming POST would look like, no chance to alter it:
Maybe I am missing something, but when an external system want to connect to you the external system must to comply with your specifications.
But anyway, in this case you will need to create an Azure Function that get the POST JSON from the external system a wrap it the way you need it and pass forward to your BC system.
Regards
Yeah I kinda thought this would be the end result - sadly. Well for the other side BC is the external system
Would've been nice to do it in full AL without bothering with an additional proxy component.
anyway, thanks a lot for your help and time @ftornero.
Regards
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, let's hope it returns something.
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, let's hope it returns something.
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread
I got really frustrated when I found out this limitation and spent some time trying to work around it without any luck. Since then I have accepted that I'm limited with three level API Page/SubPage/Complex Type, and sometimes I have also flattened data because of this. It's a nice move to create a ticket for this, I'm going to follow this thread