web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Nested API Response in Business Central

(0) ShareShare
ReportReport
Posted on by 629

Hi, 
I started working with API responses with Business Central.  The Response has nested arrays. I want to use JsonObject to get access to the last array. At this point I convert the response to text and then I manually strip the results line by line. This has some risks, but I want to use the right way with the JsonObject.

I can send the parameters to the API and I get a response. The response is converted to JsonObject. 
The problem I have is that I want to use the travel Distance in the results array but I'm unable to use the path; There is going to be N amount of results depending on the input. I want to get the value of all the TravelDistance ....The path should end with  "results[N].travelDistance".

This is what I tried thus far.
pastedimage1655309316485v1.png
The Image above gave me a list in Tokens, but its worthless.

pastedimage1655309672586v2.png

Next I tried to use the result of token as a text value to step down but  the different arrays but no luck.

I used a Jsonpath Finder to get the path that I want to use ,but BC does not recognise the path when using JObject.path('resourceSets[0].resources[0].results[2].travelDistance'); 

The path retrieved was : (x..resourceSets[0].resources[0].results[2].travelDistance) 

The response  Looks as follow:

{
"authenticationResultCode": "ValidCredentials",
"brandLogoUri": "">dev.virtualearth.net/.../logo_powered_by.png",
"copyright": "Copyright © 2022 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets": [
{
"estimatedTotal": 1,
"resources": [
{
"__type": "DistanceMatrix:http://schemas.microsoft.com/search/local/ws/rest/v1",
"destinations": [
{
"latitude": ######,
"longitude":######
},
{
"latitude": ######,
"longitude": ######
}
],
"origins": [
{
"latitude": ######,
"longitude":########
},
{
"latitude": #####,
"longitude": #####
}
],
"results": [
{
"destinationIndex": 0,
"originIndex": 0,
"totalWalkDuration": 0,
"travelDistance": 0,
"travelDuration": 0
},
{
"destinationIndex": 1,
"originIndex": 0,
"totalWalkDuration": 0,
"travelDistance": 44.109,
"travelDuration": 43.7167
},
{
"destinationIndex": 0,
"originIndex": 1,
"totalWalkDuration": 0,
"travelDistance": 43.971,
"travelDuration": 42.9
},
{
"destinationIndex": 1,
"originIndex": 1,
"totalWalkDuration": 0,
"travelDistance": 0,
"travelDuration": 0
}
]
}
]
}
],
"statusCode": ###,
"statusDescription": "OK",
"traceId": "######"
}

Thank you in Advance

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    Hi,

    I can see in your Json text there are some arrays already and you have not used any array to read those tags, arrays always defined by "[ ]" in Json text. You can hint from the below  code how to use array

    if jsObject.Get('resourceSets', Jtoken) then begin

                                               JLineContent := Jtoken.AsObject();

                                               if JLineContent.Get('results', Jtoken) then

                                                   if Jtoken.IsArray then begin

                                                       jsArray := Jtoken.AsArray();

                                                       for i := 0 to jsArray.Count - 1 do begin

                                                           jsArray.get(i, Jtoken);

                                                           if Jtoken.IsObject then begin

                                                               jsObject := Jtoken.AsObject();

                                                               if not SelectJsonToken(jsObject, 'travelDistance', false).AsValue().IsNull then

                                                                   UnitSent := SelectJsonToken(jsObject, 'travelDistance', false).AsValue().AsDecimal();

                                                           end;

                                                       end;

                                                   end;

  • Suggested answer
    Hein Kruger Profile Picture
    629 on at

    Solution

           if CheckResults(Response, url) = false then

               exit;

           ResponseObject.ReadFrom(Response);

           if ResponseObject.Get('resourceSets', ResourceSetsToken) Then begin

               if ResourceSetsToken.IsArray() then begin

                   ResourceSetsArr := ResourceSetsToken.AsArray();

                   for y := 0 to ResourceSetsArr.Count() - 1 do begin

                       ResourceSetsArr.get(y, ResourceSetsTokens);

                       if ResourceSetsTokens.IsObject() then begin

                           ResourceSetsObject := ResourceSetsTokens.AsObject();

                           if ResourceSetsObject.Get('resources', ResourcesToken) then begin

                               if ResourcesToken.IsArray() then begin

                                   ResourcesArr := ResourcesToken.AsArray();

                                   for x := 0 to ResourcesArr.Count - 1 do begin //result loop

                                       ResourcesArr.Get(x, ResourcesToken);

                                       if ResourcesToken.IsObject() then begin

                                           ResourcesObject := ResourcesToken.AsObject();

                                           if ResourcesObject.Get('results', ResourcesToken) then begin

                                               if ResourcesToken.IsArray() then begin

                                                   ResultsArr := ResourcesToken.AsArray();

                                                   for i := 0 to ResultsArr.Count - 1 do begin //result loop

                                                       ResultsArr.Get(i, ResultsToken);

                                                       if ResultsToken.IsObject() then begin

                                                           ResultsObject := ResultsToken.AsObject();

                                                           if ResultsObject.Get('destinationIndex', ResultsToken) Then begin

                                                               DestinationAPI := ResultsToken.AsValue().AsText();

                                                           end;

                                                       end;

                                                   end;

                                               end;

                                           end;

                                       end;

                                   end;

                               end;

                           end;

                       end;

                   end;

               end;

           end;

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,092

#2
YUN ZHU Profile Picture

YUN ZHU 663 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 515

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans