JSON Nested Array

Slider123Slider123 Member Posts: 7
Using a website such as https://jsonplaceholder.typicode.com/users how would I iterate through the Address array within this json text?

What would my Selecttoken path be if I wanted to retrieve the City of all available users?

Thank you.

Best Answer

Answers

  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @Slider,

    You could take a look to this post:

    https://forum.mibuso.com/discussion/comment/330760

    Regards
  • Slider123Slider123 Member Posts: 7
    @ftornero Thank you for that. In my example what is the root. In the example you provided he has item as the root.

    Thank you.
  • Slider123Slider123 Member Posts: 7
    I figured it out, but could potentially be improved.

    var
    JsonMgt: Codeunit "JSON Management";
    Counter: Integer;
    JArray: JsonArray;
    Jobject: JsonObject;
    JToken: JsonToken;
    resultToken: JsonToken;
    OutputText: Text;
    ResponseText: Text;
    returntext: Text;
    returntext2: Text;
    url: text;
    begin

    url := 'https://jsonplaceholder.typicode.com/users';
    HttpClient.Get(url, Response);

    if not Response.IsSuccessStatusCode then
    Error('Invalid Response');

    Response.Content.ReadAs(ResponseText);
    if not JToken.ReadFrom(ResponseText) then
    Error('Invalid Response');

    JsonMgt.InitializeCollection(ResponseText);
    for Counter := 0 to JsonMgt.GetCollectionCount() - 1 do begin
    JsonMgt.GetObjectFromCollectionByIndex(returntext, Counter);
    JToken.ReadFrom(returntext);
    JToken.SelectToken('address.street', resultToken);
    Message(resultToken.AsValue().AsText());
    end;
    end;
Sign In or Register to comment.