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.

posapi

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.”.

getaccesstoken

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.

setapirequest

The interesting part here is that I use an XMLPort to create the data I need to post to the Api.

apiauthenticatexml

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.

converttojson

The last TRUE variable means the the Document Element will be skipped and the Json will look like it is supposed to.

apikey

The REST service response is Json.

token

And to read the Json response we take a look at the GetAccessToken function.

getaccesstokenfunction

Here I start by converting from Json to Xml.

convertfromjson

And make sure my Document Element name is “posApi”.

apiaccesstokenxml

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

getauthorization

will read this Json

getauthorizationjson

I suggest that with our current NAV this is the easiest way to handle REST web services and Json.

 

7 thoughts on “Using REST/Json web services from NAV

  1. iris says:

    very nice article and would love to suggest too for your readers http://jsonformatter.org tools for json lovers.

  2. Michael Coscas says:

    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

    1. 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

  3. Vishal Sharma says:

    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.

    1. Hi Vishal
      In NAV pre-dotnet you will have to handle json as a text file and do everything manually.

Leave a Reply

%d bloggers like this: