Wednesday, July 2, 2014

NAV 2013 & NAV 2013 R2 - How to - Use OData to Return OR Obtain a JSON Document.

Hi all,

In Previous post related to ODATA we saw how to publish and consume a Odata Web Services from NAV 2013 & 2013 R2.

Below are some link Related to Old Articles Published for Odata -
NAV RTC - Introduction to Web Services.
Introduction of ODATA Services In NAV 2013.
What is the ODATA?
Max. Page Size in ODATA Parameter
Consuming Output of ODATA - Part 1
How to Use OData to Return OR Obtain a Service Metadata (EDMX) Document

Today we will see how to Return / Obtain a JSON Document.

What is JSON?



JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript)

Think of it as an alternative to using XML for transferring data between software components.

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

JSON Structure -


Example of JSON Format -

{
     "firstName": "Saurav",
     "lastName": "Dhyani",
     "address": {
         "streetAddress": "C-175, Block C",
         "city": "Noida",
         "state": "UP",
         "postalCode": 201301
     },
     "phoneNumbers": [
         "000 111-2222",
         "111 222-3333"
     ]
 }

Now we have a basic idea what is JSON Format, so let's see how my Odata Web Service can Return me data in this format.

The Example shown below is from NAV 2013 R2 and using a Page web Service. The Steps remain same for Query Web Services and in NAV 2013.

Create a Page Web Services for say Customer Card Page 21. 

*How to Create a Page Web Service.

For Getting data in JSON Format, Open Internet Explorer and type in URL in Below Format -

http://<Server>:<WebServicePort>/<ServerInstance>/OData/<web service>?$format=json

If Standard Installation, single company & with ODATA Web Service Name as NAVCustomers use -

http://localhost:7048/DynamicsNAV71/OData/NAVCustomers?$format=json

This generates a text file that contains metadata and data from the web service. You can open the file from the browser, or you can save it to disk as shown below.



If we open the Text file, the format will be like as we discussed above.



Remember -
The value of the format attribute must be lowercase: ?$format=json.

If you want to consume the web service as JSON-P, you can add the ?$callback=<callback function name> parameter.

I needed it and thought of sharing so that anyone else can use it, if required.

Regards,
Saurav Dhyani
saurav-nav.blogspot.com

No comments:

Post a Comment