One of my most popular blog entry is the one about Json. I have also had some questions outside this website about this topic.
This week I got a task. We need to communicate with a payment service that uses REST web services and Json file format.
I got a document describing the service. Some methods use GET and some use POST. Here is how I did this.
In the heart of it all I use Codeunit 1297, “Http Web Request Mgt.”.
Every time we talk to this POS API we send an Access Token. If we don’t have the token in memory (single instance Codeunit), we need to get a new one. That is what the above code does.
The ParameterMgt Codeunit is what I want to focus on. You can see that I start by inserting my “Authorization Key” into the RequestBodyBlob. As usual, I use the TempBlob.Blob to get and set my unstructured data.
The interesting part here is that I use an XMLPort to create the data I need to post to the Api.
A simple one in this example, but nothing says it can’t be complex. Then I convert the Xml to Json with a single function.
The last TRUE variable means the the Document Element will be skipped and the Json will look like it is supposed to.
The REST service response is Json.
And to read the Json response we take a look at the GetAccessToken function.
Here I start by converting from Json to Xml.
And make sure my Document Element name is “posApi”.
And I have the result.
As you can see from the documentation some of the Json data is more complex. This method will work nevertheless.
For more complex date I always create tables that matches the Json structure. These table I use temporary through the whole process so the don’t need to be licensed tables. Here is an example where this XMLPORT
will read this Json
I suggest that with our current NAV this is the easiest way to handle REST web services and Json.
very nice article and would love to suggest too for your readers http://jsonformatter.org tools for json lovers.
Hi Gunnar,
Thanks for your great article.
I tried to use it but I am confronting to a little problem…
I suppose that the variable JsonConvert references to the codeunit attached to your blog entry about Json, but I do not found the functions SerializeXmlNode and DeSerializeXmlNode.
Can you help me.
Thanks in advance.
Michael
It is part of the Newtonsoft.Json.JsonConvert assembly. The download attached to the “JSON meets NAV” post contains the Newtonsoft.Json.dll. This assembly contains the Newtonsoft.Json.JsonConvert class.
hth
Very nice Article.
Would there be such a thing available for older versions of NAV (5.x or 2009) that do not support .NET assemblies.
Hi Vishal
In NAV pre-dotnet you will have to handle json as a text file and do everything manually.
So nice Article
You did help me a lot, but where can I find the xmlport get function, to get the value of a specific node? I found a workaround by going through the xmldocument with system.xml objects an selectsinglenode function, but this looks a lot easier to me.
If you read everything to a temporary table the the data handling will be very easy
Hi Gunnar,
Thanks for your article, which gives me a good start for my job.
but… I need to exchange (get and post) multiple record with the REST API, and i get the error “XMLNodeConverter can only convert JSON that begins with an object” (during converting Json to XML) and “There is many root element…” (during converting from XML to JSON)…
Could you please give me a key to solve this.
Best regards.
Try to manually add a root object to your Json. { “root” : } if I remember correctly.
I had a root element, and everything is ok.
Thanks for your help.
Hello Gunner,
I am following you process and I am stuck into same issue as KTA faced.
I have json like :
{“Status”:”0″,
“AuthenticationToken”:”25a20ae-96f1-4577-9465-c0e548ceb”,
“Message”:”Errors”,
“Result”:”{
“Response
“:[{“RowNo”:”1″,
“ColumnName”:”userGstin”,
“CellValue”:”0″,
“ErrorInfo”:
“Supply Type is invalid”},
{
“RowNo”:”1″,
“ColumnName”:
“docNo”,
“CellValue”:”MV-VSD-430″,
“ErrorInfo”:”Document no MV-VSD-430 is duplicate.”
}
]}”,
“lstEWBRes”:null
}
When I use XmlDocument := JsonConvert.DeserializeXNode(Json,’EwayBillApi’);
Its only converting root elements only to XML. See below converted xml from above json.
0
25a20ae-96f1-4577-9465 c0e548ceb
Errors
{ “Response”:[{“RowNo”:”1″,”ColumnName”:”userGstin”,”CellValue”:”0″,”ErrorInfo”:”Supply Type is invalid”},{“RowNo”:”1″,”ColumnName”:”docNo”,”CellValue”:”MV-VSD-430″,”ErrorInfo”:”Document no MV-VSD-430 is duplicate.”}]}
Could you please help to resolve this issue ?
Thank in Advanced
Hi Gunnar,
I am trying integrate the NAV with the sales force.
And there also i want to use the same scenario as you used in this example.
But I am confused as I am not able to understand the codeunits you used in your code (TokenMgt, ParameterMgt).Could you please share some more details regarding these codeunits.
Thanks in Advance.
Hi Vidit
How is Sales Force authentication for REST services ?
Hi Gunnar,
It is a great article, and the method works great, but… it works great only on the client. Whenever I run it from NAS I get the client callback errors for all dotnet variables used in CU1297, despite the fact none of these variables are meant to be used on the cilent (RunOnClient is NO). Initially I though that maybe some other message is trying to appear and causing that problem, but whenever I run it from client no messages are shown. Any ideas how to fix it or work around it?
Hello Gunnar,
what is the purpose of ProcessFaultResponse method call?
Just to display the error from the REST API.
Hello Gunnar,
please share the object.
Hi
I am sorry, I cannot to that.
Hello,
I am using these functions but jsonconvert never populates the total nodes that my xmlport has.
my xmlport differs from the one you show because I have a TABLE element with Item datasource.
Any suggestion.
Sorry for my english
Hi Geronimo,
I will need to see more details, this does not ring any bells over here…
You can send me details in email.
How to pass parameter as Form-data to generate token in navision while consuming API?
Hello Gunnar,
thank you for this very informative post.
I am currently trying to access a REST API from NAV, unfortunately it does expect the types in the JSON data in the “correct” format, i.e. without quotes, e.g. { value: true } instead of { value: “true” } for boolean or { value: 123 } instead of { value: “123” } for integers.
Unfortunately JsonConvert.SerializeXmlNode always returns the values as string.
How do you handle this?
Thanks and best regards
Marc
In XML files you can only have Text values. Therefore you only get Text values when serializing Xml to Json
Thanks, that’s what I thought too.
Any recommendation on how to create correct JSON files from NAV in a simple way?
Can you use the JSON Writer Codeunit 1234?
Thanks, I will have a look at it.
Hi Gunnar, what is this URL in ProcessFaultResponse parameter?