Optimize JSON values extraction lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- tudi_x · 2017-06-16 09:12 hi All, i am trying to extract the key:value information from recurrent JSON files as per attached app. please advise if the extraction code may be optimized as the below looks complicated (in the DoURL and DoContent functions): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i v1 := TJSON Object(TJSONObject(TJSONObject(AData).Items[0]).Items[j]).Names[l];v2 := TJSONObject(TJSONObject(TJSONObject(AData).Items[0]).Items[j]).Find Path(TJSONObject(TJSONObject(TJSONObject(AData).Items[0]).Items[j]).Na mes[l]).AsString; the json looks like the below: Quote{"info": {     "users": {          "user1@gmail.com":{             "idle:": false,              "me": true,              "away": false,              "name": "david",              "idleTime:": 686,              "qubicle": "Lobby",              "favourite": false         },         "user2@gmail.com": {              "idle:": false,             "away": false,             "name": "john",             "idleTime:": 686,             "qubicle": "Lobby",             "favourite": false         },         "user3@gmail.com": {              "name": "mary",             "loggedIn": false,              "favourite": true         }     } }} ---------------------------------------------------------------------- JD · 2017-06-16 09:29 Why is users not a JSON array? ---------------------------------------------------------------------- tudi_x · 2017-06-16 09:37 these are the requirements i received. this is how the json is sent by the rest api. the json content passes the rfc 4627 validation as per https://jsonformatter.curiousconcept.com/# please advise more, should there be an issue wiht the json? ---------------------------------------------------------------------- Blestan · 2017-06-16 11:24 you can use my fpc-xon package ... json parser is fast and the internal structures are easy to understand and use try fpc-xon on github ---------------------------------------------------------------------- tudi_x · 2017-06-16 11:58 do you have an example of how i could parse a recursive JSON? i do not see an unmarshaling example on github. thank you ---------------------------------------------------------------------- Blestan · 2017-06-16 13:11 the test application just parse  json in XVAR instance and put it in a treeview ... i do not understand what u mean by recursive ... ... after parsin you can just use xon list object as a map for example  writeln(X['users'][1]['idletime'].asInteger) ---------------------------------------------------------------------- wp · 2017-06-16 13:28 Quote from: tudi_x on June 16, 2017, 01:58:21 pmdo you have an example of how i could parse a recursive JSON? You can have a look how it is done in the JSONDatastore of tvplaint:https://sourceforge.net/p/lazarus- ccr/svn/HEAD/tree/components/tvplanit/source/vpjsonds.pas: TVpJSONDatastore.ReadJSON reads an entire json file which here consists of "Resources" which, in turn, consist of arrays of "Events", "Contacts" and "Tasks". JSONToEvent, for example, extracts all information needed for an event from the corresponding json array element. I am aware that this is not a recursive algorithm because the structure of the file is well-defined. But maybe you only want to know how to parse a json in general. ---------------------------------------------------------------------- JD · 2017-06-16 14:01 Quote from: tudi_x on June 16, 2017, 01:58:21 pmdo you have an example of how i could parse a recursive JSON? i do not see an unmarshaling example on github. thank you Here is how I fill a listview using Free Pascal's JSON library. It will give you an idea about how to use JSON objects ( with {}) and JSON arrays (with []). The parser reads the string. The procedure then loops through the array and then copies the each object in the array to a line in the listview. Each value in a key/value pair will be a column on a line. It is not recursive but it works. I hope you find it useful for your purpose. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure JSONObjToListViewEx(AListVu: TListView; AJSONObj: {wide}string; AHasCheckBoxes: boolean = False);var  intGridRow, intNumberOfSQLQueryRows: integer;  //  AddItem: TListItem;  // JSON related variables  jsParser: TJSONParser;  jsArrayRow: TJSONArray;  jsObj: TJSONObject;begin  //  if AJSONObj = EmptyStr then    exit;  // create the json parser  jsParser    := TJSONParser.Create(AJSONObj);  //  try    // Parse the JSON data    jsArrayRow  := jsParser.Parse as TJSONArray;    //    try      // get the number of rows returned by the query in the JSON data object      intNumberOfSQLQueryRows := jsArrayRow.Count;      // Fill the listview from the JSON string      AListVu.Items.BeginUpdate;      AListVu.Clear;      //      for intGridRow := 0 to Pred(intNumberOfSQLQueryRows) do      begin        // assign the object to jsObj        jsObj := jsArrayRow.Objects[intGridRow];        //        try          //        AddItem := AListVu.Items.Add;          with AddItem do          begin            if AHasCheckboxes then            begin              Caption := Trim(jsObj.Items[1].AsString);              SubItems.Add(jsObj.Items[0].AsString);      // ID            end          else if not AHasCheckboxes then            begin              Caption := Trim(jsObj.Items[0].AsString);   // ID              SubItems.Add(jsObj.Items[1].AsString);            end;          end;      finally          //          jsObj.Clear;        end;      end;  finally      //      AListVu.Items.EndUpdate;      //      jsArrayRow.Free;    end;          // try...finally (jsParser := TJSONParser.Create(AJSString))  finally    // free the jsParser    FreeAndNil(jsParser);  end;end;  JD ---------------------------------------------------------------------- JD · 2017-06-16 14:40 You could also see this article on LazPlanet: A Simple JSON parsing example https://lazplanet.blogspot.fr/2014/09/a-simple-json-parsing- example.html