"requestedShipment": { "shipper": { "address": { "streetLines": [ "8181 Darrow Rd" ], "city": "Twinsburg", "stateOrProvinceCode": "OH", "postalCode": "44087", "countryCode": "US", "residential": false } }, "recipient": { "address": { "streetLines": [ "1550 Union Blvd", "Suite 302" ], "city": "Beverly Hills", "stateOrProvinceCode": "TN", "postalCode": "65247", "countryCode": "US", "residential": false } },
"requestedShipment": { "shipper": { "address": { "streetLines": [ { "": "5543 Aliquet St" //This is wrong } ], "city": "Fort Dodge", "stateOrProvinceCode": "GA", "postalCode": "20783", "countryCode": "US", "residential": "false" } }, "recipient": { "address": { "streetLines": [ { "": "7292 Dictum Ave" //This is wrong } ], "city": "San Antonio", "stateOrProvinceCode": "MI", "postalCode": "47096", "countryCode": "US", "residential": "false" } } },
WITH cduJSon DO BEGIN StartJSon; AddJSonBranch('accountNumber'); AddtoJSon('value','XXXXXXXXX'); EndJSonBranch; AddJSonBranch('rateRequestControlParameters'); AddtoJSon('returnTransitTimes',TRUE); AddtoJSon('servicesNeededOnRateFailure',FALSE); AddtoJSon('rateSortOrder','SERVICENAMETRADITIONAL'); ENDJSonBranch; AddJSonBranch('requestedShipment'); //Origin AddJSonBranch('shipper'); AddJSonBranch('address'); StartJSonArray('streetLines'); //TODO this result is malformed StartJson; AddtoJSon('','5543 Aliquet St'); //function requires name and value parameters, but name is already defined in the Array EndJSon; EndJSonArray; //streetLines AddtoJSon('city','Fort Dodge'); AddtoJSon('stateOrProvinceCode','GA'); AddtoJSon('postalCode','20783'); AddtoJSon('countryCode','US'); AddtoJSon('residential',FALSE); EndJSonBranch; //address EndJSonBranch; //shipper //Destination AddJSonBranch('recipient'); AddJSonBranch('address'); StartJSonArray('streetLines'); //TODO this result is malformed StartJSon; AddtoJSon('','7292 Dictum Ave'); //function requires name and value parameters, but name is already defined in the Array EndJSon; EndJSonArray; //streetLines AddtoJSon('city','San Antonio'); AddtoJSon('stateOrProvinceCode','MI'); AddtoJSon('postalCode','47096'); AddtoJSon('countryCode','US'); AddtoJSon('residential',FALSE); EndJSonBranch; //address EndJSonBranch; //recipient EndJSonBranch; //requestedShipment StartJSonArray('rateRequestType'); //TODO this result is malformed StartJSon; AddtoJSon('','ACCOUNT') //function requires name and value parameters, but name is already defined in the Array EndJSon; EndJSonArray; AddtoJSon('pickupType','USE_SCHEDULED_PICKUP'); StartJSonArray('requestedPackageLineItems'); StartJSon; AddJSonBranch('weight'); AddtoJSon('units','LB'); AddtoJSon('value',22); EndJSonBranch; //weight AddJSonBranch('dimensions'); AddtoJSon('length',10); AddtoJSon('width',8); AddtoJSon('height',4); AddtoJSon('units','IN'); EndJSonBranch; //dimensions; EndJSon; EndJSonArray; AddtoJSon('packagingType','YOUR_PACKAGING'); EndJSon; JSon := GetJSon;
AddJSonBranch2(BranchName : Text) JsonTextWriter.WritePropertyName(BranchName);
Name DataType Subtype Length JsonArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JsonSerialize DotNet Newtonsoft.Json.JsonSerializer.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' ArrayValue Text i Integer
AddArrayTxt2Json(VariableName : Text;ArrayTxt : ARRAY [10] OF Text) ArrayValue := '['; FOR i := 1 TO ARRAYLEN(ArrayTxt) DO BEGIN IF ArrayTxt[i] <> '' THEN IF ArrayValue = '[' THEN ArrayValue := ArrayValue + '"' + ArrayTxt[i] + '"' ELSE ArrayValue := ArrayValue + ',' + '"' + ArrayTxt[i] + '"'; END; ArrayValue := ArrayValue + ']'; JsonArray := JsonArray.Parse(ArrayValue); JsonObj := JsonObj.JObject; JsonObj.Add(VariableName, JsonArray); JsonSerialize :=JsonSerialize.JsonSerializer; JsonSerialize.Serialize(JsonTextWriter, JsonObj);
qArray[1] := '1550 Union Blv'; qArray[2] := 'Suite 302'; FunJson.StartJSon(); FunJson.AddJSonBranch('recipient'); FunJson.AddJSonBranch2('address'); FunJson.AddArrayTxt2Json('streetLines', qArray); // Not EndJSonBranch for address FunJson.EndJSonBranch(); // recipient FunJson.EndJSon; MESSAGE(FunJson.GetJSon());
Name DataType Subtype Length FunJson Codeunit Dynamics.is JSon Mgt. qArray Text
"rateRequestType": ["ACCOUNT"],
AddArrayTxtProperty2Json(VariableName : Text;ArrayTxt : ARRAY [10] OF Text) ArrayValue := '['; FOR i := 1 TO ARRAYLEN(ArrayTxt) DO BEGIN IF ArrayTxt[i] <> '' THEN IF ArrayValue = '[' THEN ArrayValue := ArrayValue + '"' + ArrayTxt[i] + '"' ELSE ArrayValue := ArrayValue + ',' + '"' + ArrayTxt[i] + '"'; END; ArrayValue := ArrayValue + ']'; JsonArray := JsonArray.Parse(ArrayValue); JsonProperty := JsonProperty.JProperty(VariableName, JsonArray); JsonSerialize :=JsonSerialize.JsonSerializer; JsonSerialize.Serialize(JsonTextWriter, JsonProperty);
Name DataType Subtype Length JsonArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JsonSerialize DotNet Newtonsoft.Json.JsonSerializer.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' ArrayValue Text i Integer JsonProperty DotNet Newtonsoft.Json.Linq.JProperty.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
Name DataType Subtype Length JsonObj DotNet Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JsonArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JsonSerialize DotNet Newtonsoft.Json.JsonSerializer.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' ArrayValue Text i Integer
AddJSonBranch('requestedShipment'); //Origin AddJSonBranch('shipper'); AddJSonBranch2('address'); CLEAR(ArrayText); ArrayText[1] := '5543 Aliquet St'; ArrayText[2] := 'Ste B'; AddArrayTxt2Json('streetLines',ArrayText); AddToJSon('city','Fort Dodge'); AddToJSon('stateOrProvinceCode','GA'); AddToJSon('postalCode','20783'); AddToJSon('countryCode','US'); AddToJSon('residential',FALSE); EndJSonBranch; //shipper //Destination AddJSonBranch('recipient'); AddJSonBranch2('address'); CLEAR(ArrayText); ArrayText[1] := '7292 Dictum Ave'; ArrayText[2] := 'Unit 400'; AddArrayTxt2Json('streetLines',ArrayText); AddToJSon('city','San Antonio'); AddToJSon('stateOrProvinceCode','MI'); AddToJSon('postalCode','47096'); AddToJSon('countryCode','US'); AddToJSon('residential',FALSE); EndJSonBranch; //recipient CLEAR(ArrayText); ArrayText[1] := 'ACCOUNT'; AddArrayTxtProperty2Json('rateRequestType',ArrayText); //ERROR HAPPENS HERE AddToJSon('pickupType','USE_SCHEDULED_PICKUP'); EndJSonBranch; //requestedShipment
Answers
You can add these 2 functions to the codeunit "Dynamics.is JSon Mgt."
The local variables in the AddArrayTxt2Json are::
And build the json like this:
The variables for the last code are:
Regards
I was able to implement the recommended functions and get the desired output for the address line details!
Note, I also had this same problem later on in my payload, so I tried to implement the same type of solution there only this time I get an error.
This part of the payload is structured a little differently. This branch exists within the same overall requested shipment branch, but at one level of indentation. In the address example, the streetlines array was for the address branch within the shipper branch (for example), within the requested shipment. This raterequesttype array is a branch directly within the requested shipment. I don't see how to use
Could you post a complete JSON example that you try to create or at least the "rateRequestType" part?
Regards.
Assuming that the "rateRequestType" is like this
You need to add a new function to the codeunit:
With these local variables
By the way I forgot to list the local variable JsonObj in the AddArrayTxt2Json function, so the complete local variables for that function are:
So the JSON part of "requestedShipment" is:
Regards
I think that you can use only that last function (AddArrayTxtProperty2Json) and the code should be this:
Regards
I was able to figure out the missing variable in the first reply, but I appreciate you updating here for future reference if anyone else has similar needs to those covered in this thread.
Can you recommend any resources that I might be able to use to learn more about working with the newtonsoft.dll? I'm sure I'll encounter more issues when I get to the point of parsing the response and I'd love to be more self-reliant.
Sorry but I don't have any documentation or resource about the newtonsoft.dll, is more about working with JSON, using Google and trial and error.
Regards.