2. Employee @ Advania (http://www.advania.is)
NAV technology, ISV, development and deployment
administration. AdvaniaGIT-SCM, Test, Build, Deploy
Community work @ Dynamics.is
Blog, workshops, sessions, collaboration
Software @ Objects4NAV (http://objects4nav.com)
Dynamics 365 and Appsource
Freelance work @ Navision.guru (http://navision.guru)
GIT, Data Exchange, Development, D365
Gunnar Gestsson has multiple hats
3. In computing, JavaScript Object Notation or JSON, is an open-standard file format
that uses human-readable text to transmit data objects consisting of attribute–
value pairs and array data types (or any other serializable value).
It is a very common data format used for asynchronous browser–server
communication, including as a replacement for XML in some AJAX-style systems.
JSON is a language-independent data format. It was derived from JavaScript, but as
of 2017 many programming languages include code to generate and parse JSON-
format data. The official Internet media type for JSON is application/json. JSON
filenames use the extension .json.
Json on Wikipedia 1
4. JSON's basic data types are:
• Number: a signed decimal number that may contain a fractional part and may use
exponential E notation, but cannot include non-numbers such as NaN. The format
makes no distinction between integer and floating-point. JavaScript uses a double-
precision floating-point format for all its numeric values, but other languages
implementing JSON may encode numbers differently.
• String: a sequence of zero or more Unicode characters. Strings are delimited with
double-quotation marks and support a backslash escaping syntax.
• Boolean: either of the values true or false
• Array: an ordered list of zero or more values, each of which may be of any type. Arrays
use square bracket notation and elements are comma-separated.
• Object: an unordered collection of name–value pairs where the names (also called keys)
keys) are strings. Since objects are intended to represent associative arrays, it is
recommended, though not required, that each key is unique within an object. Objects
are delimited with curly brackets and use commas to separate each pair, while within
each pair the colon ':' character separates the key or name from its value.
• null: An empty value, using the word null
Json on Wikipedia 2
6. • Using the Customer table in the CRONUS
• Create Json Text for columns; No., Name,
Balance
• Read the actual table (non temporary)
• Write to a temporary table
• Combine Read and Write to verify the
results
Exercises
7. LOCAL PROCEDURE OpenJson@2(Json@1003 : Text);
VAR
TempBlob@1001 : Record 99008535;
FileMgt@1000 : Codeunit 419;
BEGIN
TempBlob.WriteAsText(Json,TEXTENCODING::UTF8);
FileMgt.BLOBExport(TempBlob,'OData.json',TRUE);
END;
See your results
8. • Json is just formatted text
• Make sure to escape special
characters
• b Backspace (ascii code 08)
• f Form feed (ascii code 0C)
• n New line
• r Carriage return
• t Tab
• " Double quote
• Backslash character
LOCAL PROCEDURE EscValue@11(Value@1000 : Text) NewValue : Text;
VAR
Char@1001 : Char;
Pos@1002 : Integer;
BEGIN
IF Value = '' THEN EXIT('');
FOR Pos := 1 TO STRLEN(Value) DO BEGIN
Char := Value[Pos];
CASE Char OF
8: // Backspace
NewValue += 'b';
10: // New line
NewValue += 'n';
12: // Form feed
NewValue += 'f';
13: // Carriage return
NewValue += 'r';
9: // Tab
NewValue += 't';
34: // Double Quote
NewValue += '"';
92: // Backslash character
NewValue += '"';
ELSE
NewValue += COPYSTR(Value,Pos,1);
END;
END;
END;
Create Json manually?
9. • Start with a simple Json, for example a
copy of the Json from Wikipedia.
• Make sure the Json opens in the
correct program (I suggest Visual
Studio Code and I have Json Tools by
Erik Lynd installed in Visual Studio
Code as an extension).
• Create Json for the Customer Table
(No., Name, Balance)
• Make sure to have the correct
formatting of non-string values.
{ "Customer":
[
{
"No": "01121212",
"Name": "Spotsmeyer's Furnishings",
"Balance": "0.00"
},
{
"No": "01445544",
"Name": "Progressive Home Furnishings",
"Balance": "2310.38"
},
…
{
"No": "IC1030",
"Name": "Cronus Cardoxy Procurement",
"Balance": "0.00"
}
]
}
Exercise 1, Create Json manually
10. • Used in combination with StringBuilder and StringWriter to build
Json Text
• Json is created in a steam mode, not in object mode
• Will take care of string escape
• Codeunit 50001 on my USB
JsonTextWriter
11. • Use the Newtonsoft Wrapper Codeunit to create a Json with
Customer Data
• Results should be identical to the results in Exercise 1
• Try out the ShowJson function in Newtonsoft Wrapper Codeunit
to display the result in NAV. Try this codeunit as well for the
manually created Json.
Exercise 2, Create Json with JsonTextWriter
12. • Json Tokens
• Json Array
• Json Object
• Json Property
• Json Value
• JSON Management (Codeunit 5459)
• Object type build, not stream type build.
JObjects
13. • Create the same Customer Data Json as before, now using JSON Management
Codeunit.
• Repeat by using the Json DotNet objects without the JSON Management
Codeunit 5459
Exercise 3, Create Json with JObject
14. • Json can be converted to Xml
• Xml can be converted to Json
• Xml has a root element. Json does not need to.
• Converting from Json to Xml will always add a root element
• Converting from Xml to Json will always remove the root element
• [External] Functions will be in Codeunit 5459 in NAV 2018
• Use Codeunit 50002 from the USB
Json vs. Xml, convert to and from
15. [External]
PROCEDURE XMLTextToJSONText@29(Xml@1000 : Text) Json : Text;
VAR
XMLDOMMgt@1004 : Codeunit 6224;
JsonConvert@1003 : DotNet "'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.Newtonsoft.Json.JsonConvert";
JsonFormatting@1002 : DotNet "'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.Newtonsoft.Json.Formatting";
XmlDocument@1001 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
BEGIN
XMLDOMMgt.LoadXMLDocumentFromText(Xml,XmlDocument);
Json := JsonConvert.SerializeXmlNode(XmlDocument.DocumentElement,JsonFormatting.Indented,TRUE);
END;
To convert an Xml to Json, use
16. [External]
PROCEDURE JSONTextToXMLText@34(Json@1001 :
Text;DocumentElementName@1000 : Text) Xml : Text;
VAR
JsonConvert@1004 : DotNet "'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.Newtonsoft.Json.JsonConvert";
XmlDocument@1002 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
BEGIN
XmlDocument := JsonConvert.DeserializeXmlNode(Json,DocumentElementName);
Xml := XmlDocument.OuterXml;
END;
To convert a Json to an Xml, use
18. • We are familiar with the Xml layout
• We already know how to use Xml DOM
Codeunit 6224
• We have legacy code that created Xml from out
data
• We can use XmlPorts
Why converting between Json and Xml
19. Pros
• Limit the no. of code lines
• Will convert to AL and work in
Extension V2
• Combination of XmlPorts and
temporary table will map your data to
and from NAV structure fast and easy
Cons
• More NAV Objects
• Does not support Json value types
when writing Json since Xml only
supports Text values
Pros and Cons with XmlPorts
20. • Create a temporary table that will
match your Json
• Create an XmlPort to read/write that
temporary table
• Populate the temporary table from
the Customer Table to create the Xml
• Convert that Xml to Json, open and
compare to the previous exercises
• XmlPort uses Temporary Tables, add
[External]Get and [External]Set
functions to copy a temporary table
into and from the XmlPort
• XmlPort should use UFT-8
• XmlPort formatting should be Xml
• Remember a single Root Element
before adding the table data
• Use TempBlob.Blob to stream the data
in and out
Exercise 4, Create Json with XmlPort
21. • Manually – but I would not do that
• Convert Json to Xml and read with XmlPort and/or DOM
Codeunit 6224
• Use JsonTextReader to read Json to a buffer table (Json Buffer,
1236)
• Use JObject DotNet to read Json
Json readers
22. • Create a reader and try to read all of the four Json you
have already created.
• Try XmlPort reader, JsonTextReader and JObject DotNet
using the methods found in JSON Management Codeunit
5459
Exercise 5, Try different Json readers
23. • AL Comes with Json data types
• JsonObject
• JsonArray
• JsonToken
• JsonValue
• Based of the DotNet JObject we just used
• Simplified syntax
• Type conversion to native AL & C/AL data types
AL in Visual Studio Code
24. • Use the built in data types to create the customer Json in AL
• Also use the built in data types to read that customer Json back
into a temporary table and display to the user
Exercise 6, Create and read Json in AL
25. Add $format=json to the query
https://spla2016odata.navleiga.is/Kappi/OData/Company('Kappi%20ehf.')/InsightAc
countingPeriods?tenant=kappi&$format=json
Use
https://msdn.microsoft.com/en-us/library/dn182582(v=nav.90).aspx
http://www.kauffmann.nl/2015/11/26/web-services-examples-part-1-the-basic-
pattern/
Json in Odata services