Find duplicate objects in JSON Array of Objects lazarus / 21-General ---------------------------------------------------------------------- ELCouz · 2022-03-19 17:21 Hi, I am trying to find the best possible way to find duplicate objects and return the object path of the duplicated objects. Let say I have this simplified JSON : Code: [Select][     {         "address": {             "x": 55,             "y": 1         },         "id": "c4:f3:12:70:e9:76",     },     {         "address": {             "x": 55,             "y": 2         },         "id": "c4:f3:12:72:29:d4",     },     {         "address": {             "x": 55,             "y": 2         },         "id": "9c:1d:58:62:d5:7b"     },     {         "address": {             "x": 55,             "y": 3         },         "id": "9c:1d:58:62:d5:7c"     } ] There can be only one X and Y combination possible and if they are same X & Y return the object id in the JSON string. So in this case that would be object id 1 & 2 that I need to get. They are over 500 objects to scan for duplicate in the real JSON string. I thought of iterating every JSON Object with comparing each X and Y with all the other objects but I don't think this is efficient at all. Do you have an idea what could be the best way to accomplish this ? Thanks a lot! Laurent ---------------------------------------------------------------------- alpine · 2022-03-19 18:37 Use TJSONArray.Sort(@SortXY) and define SortXY like: 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 function SortXY(Item1, Item2: Pointer): Integer; begin  Result :=     TJSONObject(Item1).Objects['address'].Integers['x'] -     TJSONObject(Item2).Objects['address'].Integers['x'];  if Result = 0 then    Result :=       TJSONObject(Item1).Objects['address'].Integers['y'] -       TJSONObject(Item2).Objects['address'].Integers['y'];end;  (Refactor it for single prop search) Then duplicated objects will be one after another and you can perform just a single scan. You didn't define what the "best possible way" means. ---------------------------------------------------------------------- Zvoni · 2022-03-21 08:36 Or load the "inner" JSONObject as a simple String into a StringList with  no duplicates allowed and set the "id"-field as the Object of the StringList-Entry. The question remains: Which "id" would you use/is valid for x=55, y=2? ---------------------------------------------------------------------- ELCouz · 2022-03-22 15:55 Quote from: y.ivanov on March 19, 2022, 07:37:15 pmYou didn't define what the "best possible way" means. I meant as an efficient way to scan for objects in the JSON. I will test this later on. Looks good. Thanks! Quote from: Zvoni on March 21, 2022, 09:36:15 amThe question remains: Which "id" would you use/is valid for x=55, y=2? I will query both duplicate and find out which is still valid. The API is not perfect and I need to mitigate the duplicate which should not happen. There can only one robot present on a rail, just need to ping which of the two is active and ignore the other object until the controller stop sending the old ID (robot that as been taken off the rail and replaced by a new one) Thanks guys for the help, appreciated! Laurent