JSONBuffer

Dear all,

I'm trying to read a JSon File withing the JsonBuffer table.

The structure of my Json is the following :

ljlscqbbu9z4.png

What i'm trying to achieve is getting the coordinates out of the Json file to store into database fields.

I'm trying to achieve this by the following code :

codeunit 50112 HttpGeoCodingMgmt
{
    procedure GetLongLat()
    var
        uri: Text;
        ResponseInReadibleFormat: Text;
    begin
        uri := 'http://dev.virtualearth.net/REST/v1/Locations/*******************************';
        CallExternalWebService(uri, ResponseInReadibleFormat);
        GetResultofWebservice(ResponseInReadibleFormat);
    end;

    local procedure CallExternalWebService(uri: Text; var ResponseInReadibleFormat: Text)
    var
        RequestMessage: HttpRequestMessage;
        HttpClient: HttpClient;
        ResponseMessage: HttpResponseMessage;
        Content: HttpContent;
    begin
        RequestMessage.Method := 'GET';
        RequestMessage.SetRequestUri(uri);
        HttpClient.Send(RequestMessage, ResponseMessage);
        Content := ResponseMessage.Content;
        Content.ReadAs(ResponseInReadibleFormat);
    end;

    local procedure GetResultOfWebservice(var ResponseInReadableFormat: Text)
    var
        JsonBuffer: Record "JSON Buffer" temporary;
        TokenSearchText: text;
        GeoCodeResult: Text;

    begin
        TokenSearchText := 'coordinates';
        JsonBuffer.ReadFromText(ResponseInReadableFormat);
        // JsonBuffer.GetPropertyValue(GeoCodeResult, TokenSearchText);
        // message('%1',GeoCodeResult);
        JsonBuffer.FindArray(JsonBuffer, TokenSearchText);

    end;


}

My JSONBuffer containsfollowing records :

lwzsd3cvdufb.png

Hopefully, someone can tell me what i'm doing wrong or what i have to do too get the coordinates as result.

Kind regards

Steve

Answers

  • ShaiHuludShaiHulud Member Posts: 228
    edited 2020-06-04
    Hey Steve,

    If you look at the JSON Buffer table, you can find the definitions of the functions GetPropertyValue, GetPropertyValueAtPath and FindArray
    nk2a93bovyxa.png

    The first attempt, using GetPropertyValue doesn't work because it finds your "coordinates" element (Entry No. = 31) and then returns the value of the next (Entry No +1) element, which is Entry No. 32 and only describes the start of the array. While you need elements 33 and 34.

    Using FindArray doesn't succeed because the path is not found (it looks for "coordinates[*" and can't find any).

    You could use GetPropertyValueAtPath with the precise path you know "resourceSets[0].resources[0].point.coordinates[0]" and "<..>coordinates[1]", or play around with arrays, going down one level at a time. Alternatively, you could make your own implementation of these functions to suit your needs
Sign In or Register to comment.