XML to JSON forcing XMLNode to be an Array

Remco_Reinking
Member Posts: 74
When using XMLPorts and NewtonSoft to create a json message where we have nested nodes these should be converted to a json array. But when such a node only contains one "record" it will not convert to a json array.
How to get this working is explained at the newtonsoft website (https://newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray.htm)
But doing so is not possible only using a xmlport.
I could not find any example to accomplish this, so here it is.
Where
How to get this working is explained at the newtonsoft website (https://newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray.htm)
But doing so is not possible only using a xmlport.
I could not find any example to accomplish this, so here it is.
Where
TreatAsArray := '//role';when we would have the same sample as on the newtonsoft website.
[TryFunction] ExportXmlPort2Json(ThisRecAsVar : Variant;XMLPortNo : Integer;VAR JsonResult : DotNet "System.String") begin TempBlob.Blob.CREATEOUTSTREAM( OutStr); XMLPORT.EXPORT( XMLPortNo, OutStr, ThisRecAsVar); ConvertXMLToJson(TempBlob, JsonResult); end; function ConvertXMLToJson(VAR TempBlob : TEMPORARY Record TempBlob;VAR Json : DotNet "System.String") begin TempBlob.Blob.CREATEINSTREAM(InStr); XmlDocument := XmlDocument.XmlDocument; XmlDocument.Load(InStr); IF TreatAsArray <> '' THEN BEGIN XmlAtt := XmlDocument.CreateAttribute( 'json', 'Array','http://james.newtonking.com/projects/json'); XmlAtt.InnerText := 'true'; WHILE STRPOS(TreatAsArray,';') >0 DO // to allow multiple nodes to be marked as an array. BEGIN ArrayNode := COPYSTR( TreatAsArray, 1, STRPOS(TreatAsArray,';')-1); TreatAsArray := COPYSTR( TreatAsArray, STRPOS(TreatAsArray,';')+1, STRLEN(TreatAsArray)); XMLNode := XmlDocument.SelectSingleNode( ArrayNode); XMLNode.Attributes.Append( XmlAtt); END; ArrayNode := TreatAsArray; XMLNode := XmlDocument.SelectSingleNode( ArrayNode); // xpath notation XMLNode.Attributes.Append( XmlAtt); END; Json := JSONConvert.SerializeXmlNode(XmlDocument.DocumentElement,JSONFormatting.Indented,TRUE); end;
0
Comments
-
Hi Remco,
I handled similar situation earlier, may be my idea help you. I don't have example ready with me.
Create extra Node with same name where source as Text in XMLport. In JSON this will create Array. In JSON if you see extra character that you wist to remove then use something like this:
// Special handling
DotNetString := DotNetString.Replace('},""]}}}','}]}}}');
Do let us know if this is helpful?0 -
Hi sunmorningindia,
there was no question in my post, just a small piece of code to share. I wanted to have a generic solution, where the XMLPort is coming from a setup table, together with the TreatAsArray variable.
Doing so allows me to have the XMLPort have the same elements as needed in the json structure.
0 -
sunmorningindia wrote: »Hi Remco,
I handled similar situation earlier, may be my idea help you. I don't have example ready with me.
Create extra Node with same name where source as Text in XMLport. In JSON this will create Array. In JSON if you see extra character that you wist to remove then use something like this:
// Special handling
DotNetString := DotNetString.Replace('},""]}}}','}]}}}');
Do let us know if this is helpful?
HI, if I use this solution I get always a null at the beginning of the list.
I have this XMLPort
But when I convert it to json I get
How can I get rid of that null?
Thanks in advance,
NS===============
Nuno Silva0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions