Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

Nested API Response in Business Central

Posted on by 501

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

  • Suggested answer
    Hein Kruger Profile Picture
    Hein Kruger 501 on at
    RE: Nested API Response in Business Central

    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;

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Nested API Response in Business Central

    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;

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans