Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV forum
Unanswered

Adding nodes with JsonTextWritter

Posted on by 215

Hi everyone,
Probably is an easy question, but I can't find the way to do this. This is the structure of an XML

The resultant Json should be like this:

{  "@xmlns": "<urn:microsoft-dynamics-nav/xmlports/eShopItem",  
"eShopItem": [    
{      "@Ref": "4802-31",      
		"@DeleteForEshop": "false",      
		"@Picture1": "http://www.xxxxxx.com/epicture/xx/4802-31.jpg",      
		"@Category": "Ixxxx",      
		"@Subcategory": "",      
		"@SubcDescription": "",      
		"@Range": "24\"",      
		"@Reading": "",      
		"@WeightKg": "0.070",      
		"ByShop": [        
			{          "@eShopURL": "eShop1",          
						"@Enabled": "true",          
						"@Description": "RADIUS GAGE",          
						"@Availability": "8",          
						"@GenericPrice": "0.00",          
						"@InTransit": "50",          
						"@FromTransitDate": "2019-05-20",          
						"@InCentral": "0",          
						"@FromCentralDate": "",          
						"@FromCentralDateText": "",          
						"@URLTechnicalCard": "http://www.xxxx.com/pdf/xxx/4802.pdf"        },        
			{          "@eShopURL": "eShop2",          
						"@Enabled": "true",          
						"@Description": "Medidor de Radios 25-40 mm",          
						"@Availability": "8",          
						"@GenericPrice": "0.00",          
						"@InTransit": "50",          
						"@FromTransitDate": "2019-05-20",          
						"@InCentral": "0",          
						"@FromCentralDate": "",          
						"@FromCentralDateText": "",          
						"@URLTechnicalCard": "http://www.xxxxxx.com/pdf/xxxxx/esp/4802.pdf"        }     
				]    
}


And I'm trying to code like this:

LOCAL CreateSimpleJsonFile(VAR JSonResponse : DotNet "Newtonsoft.Json.Linq.JObject";peShopItem : Record "eShop Item")

  JSonResponse := JSonResponse.JObject();
  JsonTextWriter := JSonResponse.CreateWriter();
  JsonTextWriter.WritePropertyName('Ref');
  JsonTextWriter.WriteValue(peShopItem."No.");
  JsonTextWriter.WritePropertyName('DeleForEshop');
  JsonTextWriter.WriteValue(peShopItem."Delete-for Eshop");
  JsonTextWriter.WritePropertyName('Picture1');
  JsonTextWriter.WriteValue(peShopItem."Picture 1");
  JsonTextWriter.WritePropertyName('Category');
  JsonTextWriter.WriteValue(peShopItem."Item Category Code");
  JsonTextWriter.WritePropertyName('Subcategory');
  JsonTextWriter.WriteValue(peShopItem."Product Group Code");
  JsonTextWriter.WritePropertyName('SubcDescription');
  JsonTextWriter.WriteValue(peShopItem."Item Group Description");
  JsonTextWriter.WritePropertyName('Range');
  JsonTextWriter.WriteValue(peShopItem.Range);
  JsonTextWriter.WritePropertyName('Reading');
  JsonTextWriter.WriteValue(peShopItem.Reading);
  JsonTextWriter.WritePropertyName('WeightKg');
  JsonTextWriter.WriteValue(peShopItem."Weight (Kg)");
  leShopItembyShop.RESET;
  leShopItembyShop.SETRANGE("Item No.",peShopItem."No.");
  IF leShopItembyShop.FINDSET THEN REPEAT
    JsonTextWriter.WritePropertyName('eShopURL');
    JsonTextWriter.WriteValue(leShopItembyShop."eShop URL");
    JsonTextWriter.WritePropertyName('Enabled');
    JsonTextWriter.WriteValue(leShopItembyShop.Enabled);
    JsonTextWriter.WritePropertyName('Description');
    JsonTextWriter.WriteValue(leShopItembyShop.Description);
    JsonTextWriter.WritePropertyName('Availability');
    JsonTextWriter.WriteValue(leShopItembyShop.Availability);
    JsonTextWriter.WritePropertyName('GenericPrice');
    JsonTextWriter.WriteValue(leShopItembyShop."Generic Price");
    JsonTextWriter.WritePropertyName('InTransit');
    JsonTextWriter.WriteValue(leShopItembyShop."Qty. on Purch. Order");
    JsonTextWriter.WritePropertyName('FromTransitDate');
    JsonTextWriter.WriteValue(leShopItembyShop."Next Reception Date");
    JsonTextWriter.WritePropertyName('InCentral');
    JsonTextWriter.WriteValue(leShopItembyShop."Qty. on Origin");
    JsonTextWriter.WritePropertyName('FromCentralDate');
    JsonTextWriter.WriteValue(leShopItembyShop."Recep. Date-from Origin");
    JsonTextWriter.WritePropertyName('FromCentralDateText');
    JsonTextWriter.WriteValue(leShopItembyShop."Recep.-from Origin (Text)");
    JsonTextWriter.WritePropertyName('URLTechnicalCard');
    JsonTextWriter.WriteValue(leShopItembyShop."Technical Card url");
  UNTIL leShopItembyShop.NEXT = 0;

With this code, I obtain this file:

{
  "Ref": "1108-150",
  "DeleForEshop": false,
  "Picture1": "www.xxxx.com/.../1108-150W.jpg",
  "Category": "INSIZE",
  "Subcategory": "02",
  "SubcDescription": "Calibres",
  "Range": "0-150mm/0-6\"",
  "Reading": "0.01mm/0.0005\"",
  "WeightKg": 0.3875,
  "eShopURL": "eShop2",
  "Enabled": true,
  "Description": "Calibre Digital 0-150mm/0-6\"",
  "Availability": 335.0,
  "GenericPrice": 0.0,
  "InTransit": 0.0,
  "FromTransitDate": "0001-01-01T00:00:00",
  "InCentral": 0.0,
  "FromCentralDate": "0001-01-01T00:00:00",
  "FromCentralDateText": "",
  "URLTechnicalCard": "www.xxx.com/.../1108.pdf"
}{
  "Ref": "1108-150W",
  "DeleForEshop": false,
  "Picture1": "www.xxxx.com/.../1108-150W.JPG",
  "Category": "INSIZE",
  "Subcategory": "02",
  "SubcDescription": "Calibres",
  "Range": "0-150mm/0-6\"",
  "Reading": "0.01mm/0.0005\"",
  "WeightKg": 0.3835,
  "eShopURL": "eShop2",
  "Enabled": true,
  "Description": "Calibre Digital Sin Ruleta 0-150mm/0-6\"",
  "Availability": 585.0,
  "GenericPrice": 0.0,
  "InTransit": 0.0,
  "FromTransitDate": "0001-01-01T00:00:00",
  "InCentral": 0.0,
  "FromCentralDate": "0001-01-01T00:00:00",
  "FromCentralDateText": "",
  "URLTechnicalCard": "www.xxxx.com/.../1108.pdf"
}{


As you can imagine, after the findset I should create another deeper level of the json, but this is not working. What sentence should I use?

Thank you very much

Categories:

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans