dynamic read on JSON response in c#

If you want to work with a JSON-formatted response, there are many ways to handle this. In the following I could figure out a dynamic read that will give you the opportunity to use the different types.

The first code-example shows how to download some data with a given URL. For this you will need the assembly “System.Net”:

string url = "address?$format=json";
var webClient = new WebClient();
String jsonResponse = webClient.DownloadString(url);

The second code-example shows how to read out the single attributes. For this you have to include the assembly “Newtonsoft.Json”:

dynamic results = JsonConvert.DeserializeObject(jsonResponse);

string myName = results.Name;
string myFirstName = results.FirstName;
int myAge = results.Age;

After using the JsonConvert.DeserializeObject-function you can use every single attribute.


JSON response via NAV

#This article will work with NAV 2016!

To get a JSON Response via NAV we can create a webservice-codeunit and publish it. Following this we probably use SOAP to publish the functions and will get a XML-response. In some projects it is easier to handle a JSON response. As well there are opportunities to read out a NAV response with the JSON format. The easiest way to do this is by publishing a page. To do this just go to the table “Web Service” (2000000076) and create an entry for a page. If you run this via the normal user-client (search for “web service”) you will see a Odata-link. This link is generated automatic. If you set on the Odata on the service tier and use this link you will get a response with the data of the page.

Probably it is xml-format but you can add the following at the end of you Odata-url:


Now your response should use the JSON-format.

If you want to do the same with a codeunit you will get no odata-link.  This is because we use a REST-service. A REST-service is done to have the basic-handler GET, POST, PUT, … . So if you would try to write some codeunit that should provide a json-response, the codeunit had to handle all of this basic-statements. A codeunit in NAV is not made to provide data like a real REST-service. But if you want to use data that way -> publish the data with a page and you can work with this.