Error during Json parsing using Newtonsoft .net dll

nikhiltoor27
Member Posts: 8
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?
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?
0
Answers
-
Hello @nikhiltoor27 ,
I already answered you here:
https://forum.mibuso.com/discussion/75008/using-newtonsoft-json-library-to-find-specific-key-value-pairs-in-a-json-string#latest
But in this code example you need to change
This:ArrayString := JObject.SelectToken('packingBoxDetails').ToString;
To this:ArrayString := JObject.SelectToken('packDoneDetails[0].packingBoxDetails').ToString;
Regards1
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