Error during Json parsing using Newtonsoft .net dll

Hi,

I am using the Newtonsoft dll to parse the json response I am receiving from an external API. During the parsing the first two arrays packdonedetails and orderdetails are parsed successfully. But when parsing the packingBoxDetails array I am getting the error that

Microsoft Dynamics NAV

Break On Error Message:

A DotNet variable has not been instantiated. Attempting to call ToString in CodeUnit MAVEN Integration: ParseJSONResponse
OK

This is the function that I've deisgned with the response that I am parsing.

Response
{
"ordersQueriedcount": 1,
"ordersFetchedcount": 1,
"ordersNotFoundcount": 0,
"error": null,
"packDoneDetails": [
{
"orderNumber": "12T2000000N",
"orderReference": "20793064251",
"systemOrderDate": "2020-02-27T16:01:41.000",
"channelOrderDate": "2020-02-27T21:31:41.000",
"packDoneDate": "2020-02-27T16:05:07.000",
"orderType": "Sale Order",
"destType": "Customer",
"destCode": "NBC00013883",
"destAddress": "3rd Floor Chitrakut Apartments,",
"destName": "Sakshi",
"destCity": "BARODA",
"destPinCode": "390007",
"shippingAddressCode": "STA0000003",
"billingAddressCode": null,
"sourceType": "Warehouse",
"sourceCode": null,
"sourceName": "Nicobar Warehouse Faridabad",
"sourceCity": "Faridabad",
"sourcePinCode": "121001",
"channelName": "Maven Manual",
"orderItems": [
{
"skuCode": "NICOTEST",
"skuName": "Nicotest",
"skuAlternateCode": null,
"skuUpcean": null,
"quantity": 7,
"usnList": "[]",
"channelCode": null,
"price": 950.0,
"mrp": 0.0,
"batch": null
},
{
"skuCode": "NICOTEST",
"skuName": "Nicotest",
"skuAlternateCode": null,
"skuUpcean": null,
"quantity": 4,
"usnList": "[]",
"channelCode": null,
"price": 950.0,
"mrp": 0.0,
"batch": null
}
],
"packingBoxDetails": [
{
"boxNumber": "BOXWX00000008",
"lengthsi": null,
"widthsi": null,
"heightsi": null,
"weightsi": 1.0,
"volweightsi": 1.0,
"orderItems": [
{
"skuCode": "NICOTEST",
"skuName": "Nicotest",
"skuAlternateCode": null,
"skuUpcean": null,
"quantity": 7,
"usnList": "[]",
"channelCode": null
}
]
},
{
"boxNumber": "BOXWX00000009",
"lengthsi": null,
"widthsi": null,
"heightsi": null,
"weightsi": 1.0,
"volweightsi": 1.0,
"orderItems": [
{
"skuCode": "NICOTEST",
"skuName": "Nicotest",
"skuAlternateCode": null,
"skuUpcean": null,
"quantity": 4,
"usnList": "[]",
"channelCode": null
}
]
}
]
}
]
}

Function
ParseJsonResponse(Value:Text)

Name DataType Subtype Length
JSONManagement Codeunit JSON Management
JsonArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
JObject DotNet Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json'
ArrayString Text
NavOrderNo Text
MavenOrderNo Text
MavenOrderType Text
MAVENbufferTable Record MAVEN buffer Table
EntryNo Integer
NoOfBoxes Integer
BoxNumber Text
TotalWeight Decimal
WeightSI Text
BoxWeight Decimal


JSONManagement.InitializeFromString(Value);
JSONManagement.GetJSONObject(JObject);
ArrayString := JObject.SelectToken('packDoneDetails').ToString;

//Read and Parse JSONArray
CLEAR(JSONManagement);
CLEAR(JObject);
JSONManagement.InitializeCollection(ArrayString);
JSONManagement.GetJsonArray(JsonArray);
CLEAR(MavenOrderNo);
CLEAR(NavOrderNo);
CLEAR(MavenOrderType);
//loop and can do insert
FOREACH JObject IN JsonArray DO BEGIN
MavenOrderNo := FORMAT(JObject.GetValue('orderNumber'));
NavOrderNo := FORMAT(JObject.GetValue('orderReference'));
MavenOrderType := FORMAT(JObject.GetValue('orderType'));
END;

ArrayString := JObject.SelectToken('orderItems').ToString;
CLEAR(JSONManagement);
CLEAR(JObject);
JSONManagement.InitializeCollection(ArrayString);
JSONManagement.GetJsonArray(JsonArray);

FOREACH JObject IN JsonArray DO BEGIN
IF MAVENbufferTable.FINDLAST THEN
EntryNo := MAVENbufferTable."Entry No." + 1
ELSE
EntryNo := 1;
MAVENbufferTable.INIT;
MAVENbufferTable."Entry No." := EntryNo;
MAVENbufferTable."API Type" := MAVENbufferTable."API Type"::"Sales Pack Done";
MAVENbufferTable."Maven Order Type" := MAVENbufferTable."Maven Order Type"::"Sales Order";
MAVENbufferTable."Maven Document No." := MavenOrderNo;
MAVENbufferTable."NAV Document No." := NavOrderNo;
MAVENbufferTable."Item No." := FORMAT(JObject.GetValue('skuCode'));
EVALUATE(MAVENbufferTable."Qty. Fulfilled",FORMAT(JObject.GetValue('quantity')));
MAVENbufferTable.INSERT;
END;

JSONManagement.InitializeFromString(Value);
JSONManagement.GetJSONObject(JObject);
ArrayString := JObject.SelectToken('packingBoxDetails').ToString;
//ArrayString := JObject.SelectToken('packDoneDetails').ToString;

CLEAR(JSONManagement);
CLEAR(JObject);
JSONManagement.InitializeCollection(ArrayString);
JSONManagement.GetJsonArray(JsonArray);
CLEAR(NoOfBoxes);
CLEAR(TotalWeight);
FOREACH JObject IN JsonArray DO BEGIN
BoxNumber := FORMAT(JObject.GetValue('boxNumber'));
IF BoxNumber <> '' THEN
NoOfBoxes += 1;


WeightSI := FORMAT(JObject.GetValue('weightsi'));
IF WeightSI <> '' THEN BEGIN
EVALUATE(BoxWeight,WeightSI);
TotalWeight += BoxWeight;
END;
END;

MAVENbufferTable.RESET;
MAVENbufferTable.SETRANGE("Maven Document No.",MavenOrderNo);
MAVENbufferTable.SETRANGE("NAV Document No.",NavOrderNo);
MAVENbufferTable.SETFILTER("API Type",'%1',MAVENbufferTable."API Type"::"Sales Pack Done");
MAVENbufferTable.SETFILTER("No. of Boxes",'=%1',0);
IF MAVENbufferTable.FINDSET THEN
REPEAT
MAVENbufferTable."No. of Boxes" := NoOfBoxes;
MAVENbufferTable."Package Weight" := TotalWeight;
MAVENbufferTable.MODIFY;
UNTIL MAVENbufferTable.NEXT = 0;

Can please someone help me out where I am doing wrong?

Answers

Sign In or Register to comment.