Extract pair of values from a json object.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Extract pair of values from a json object.

Post by KilliK » 26 Jan 2023, 06:14

Hello.

Chrome's bookmarks file is a json file with the below hierarchy:

Code: Select all

{
   "checksum": "688c795c82968728c5d81a50d8dc10f0",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13319166277591147",
            "id": "11",
            "meta_info": {
               "last_visited_desktop": "13319166277591334"
            },
            "name": "Yandex",
            "type": "url",
            "url": "https://yandex.com/"
         } ],
         "date_added": "13319166157194773",
         "date_modified": "13319166284776317",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [ {
            "children": [ {
               "date_added": "13319166167629700",
               "id": "6",
               "meta_info": {
                  "last_visited_desktop": "13319166167629868"
               },
               "name": "Google",
               "type": "url",
               "url": "https://www.google.com/"
            }, {
               "children": [ {
                  "date_added": "13319166209639295",
                  "id": "9",
                  "meta_info": {
                     "last_visited_desktop": "13319166209639443"
                  },
                  "name": "Bing",
                  "type": "url",
                  "url": "https://www.bing.com/?toHttps=1&redig=1A29E89D0713485780A5D1DA819339B5"
               } ],
               "date_added": "13319166186436779",
               "date_modified": "13319166247271113",
               "id": "7",
               "name": "FOLDER2",
               "type": "folder"
            } ],
            "date_added": "13319166167627517",
            "date_modified": "13319166186436991",
            "id": "5",
            "name": "FOLDER1",
            "type": "folder"
         }, {
            "date_added": "13319166247271113",
            "id": "10",
            "meta_info": {
               "last_visited_desktop": "13319166247271383"
            },
            "name": "DuckDuckGo",
            "type": "url",
            "url": "https://duckduckgo.com/"
         } ],
         "date_added": "13319166157194783",
         "date_modified": "13319166277591147",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13319166157194784",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}
i want to export the bookmarks using AHK, instead of the browser's native function.
i am not familiar with objects and recursive loops, and so far this is the code i came up with using the json2ahk library:

Code: Select all

#Include JSON AHK1.ahk

FileRead, textv, bookmarks
Array:= Jxon_Load(textv)
Search(Array)
Return

Search(Array) {
for each, obj in Array
 {
msgbox key: %each%
if !IsObject(obj)
msgbox value: %obj%
else 
  {
msgbox RECURSE
search(obj)
  }
 }
}
it does show all the keys and values in the entire bookmark tree, but i dont know how to continue from there.
the problem is that each loop creates a new loop and so on, and i lose track of which values of bookmarks (url/title) belong to which children object, which corresponds to the folder which contains the bookmarks. also the object that provides information (folder name) about the children object comes in the end after the bookmarks properties.
can someone help me with this? how to create the pairs url-title-folder_name in their corresponding children groups? i have found some guides in stackoverflow to export the bookmarks, but they use python or the jq app. i would like to do this with AHK.

User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Extract pair of values from a json object.

Post by Xeo786 » 26 Jan 2023, 07:49

why not use Regex if json object is complicated

Code: Select all

gosub, loadJson ; loading json var
; expressesion '=' and ':='
param1 = "url":\s"([.:\d+\w\/\?=`&`%]+)"
param2 := chr(34) "url" chr(34) ":\s" chr(34) "([.:\d+\w\/\?=`&`%]+)" chr(34)
if(param1 != param2)
  msgbox, param 1 and 2 are not same
s := 1 ; match start point
while (s := regexmatch(x,param1,f,s + StrLen(f)))
{
  msgbox, % "url: " f1 "`nStrNum:" s
}

loadJson:
x =
(
  {
   "checksum": "688c795c82968728c5d81a50d8dc10f0",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13319166277591147",
            "id": "11",
            "meta_info": {
               "last_visited_desktop": "13319166277591334"
            },
            "name": "Yandex",
            "type": "url",
            "url": "https://yandex.com/"
         } ],
         "date_added": "13319166157194773",
         "date_modified": "13319166284776317",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [ {
            "children": [ {
               "date_added": "13319166167629700",
               "id": "6",
               "meta_info": {
                  "last_visited_desktop": "13319166167629868"
               },
               "name": "Google",
               "type": "url",
               "url": "https://www.google.com/"
            }, {
               "children": [ {
                  "date_added": "13319166209639295",
                  "id": "9",
                  "meta_info": {
                     "last_visited_desktop": "13319166209639443"
                  },
                  "name": "Bing",
                  "type": "url",
                  "url": "https://www.bing.com/?toHttps=1&redig=1A29E89D0713485780A5D1DA819339B5"
               } ],
               "date_added": "13319166186436779",
               "date_modified": "13319166247271113",
               "id": "7",
               "name": "FOLDER2",
               "type": "folder"
            } ],
            "date_added": "13319166167627517",
            "date_modified": "13319166186436991",
            "id": "5",
            "name": "FOLDER1",
            "type": "folder"
         }, {
            "date_added": "13319166247271113",
            "id": "10",
            "meta_info": {
               "last_visited_desktop": "13319166247271383"
            },
            "name": "DuckDuckGo",
            "type": "url",
            "url": "https://duckduckgo.com/"
         } ],
         "date_added": "13319166157194783",
         "date_modified": "13319166277591147",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13319166157194784",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}
)
return
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: Extract pair of values from a json object.

Post by Datapoint » 26 Jan 2023, 14:52

Code: Select all

#Include Jxon.ahk
gosub, getJson
Bookmarks := Jxon_Load(x)
EnumerateFolder(Bookmarks.roots, "", 0, Output := {})
for key, val in Output
    MsgBox % "Name: " val.Name "`nURL: " val.URL "`nFolder Name: " val.FolderName
return

EnumerateFolder(obj, FolderName, depth, Output)
{
    for key, val in obj
    {
        if (val.Type = "folder")
            EnumerateFolder(val.children, FolderName " --> " val.name, ++depth, Output)
        else if (val.HasKey("url"))
            Output.Push( {"Name": val.name, "URL": val.url, "FolderName": FolderName} )
    }
    return Output
}

getJson:
x =
(
  {
   "checksum": "688c795c82968728c5d81a50d8dc10f0",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13319166277591147",
            "id": "11",
            "meta_info": {
               "last_visited_desktop": "13319166277591334"
            },
            "name": "Yandex",
            "type": "url",
            "url": "https://yandex.com/"
         } ],
         "date_added": "13319166157194773",
         "date_modified": "13319166284776317",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [ {
            "children": [ {
               "date_added": "13319166167629700",
               "id": "6",
               "meta_info": {
                  "last_visited_desktop": "13319166167629868"
               },
               "name": "Google",
               "type": "url",
               "url": "https://www.google.com/"
            }, {
               "children": [ {
                  "date_added": "13319166209639295",
                  "id": "9",
                  "meta_info": {
                     "last_visited_desktop": "13319166209639443"
                  },
                  "name": "Bing",
                  "type": "url",
                  "url": "https://www.bing.com/?toHttps=1&redig=1A29E89D0713485780A5D1DA819339B5"
               } ],
               "date_added": "13319166186436779",
               "date_modified": "13319166247271113",
               "id": "7",
               "name": "FOLDER2",
               "type": "folder"
            } ],
            "date_added": "13319166167627517",
            "date_modified": "13319166186436991",
            "id": "5",
            "name": "FOLDER1",
            "type": "folder"
         }, {
            "date_added": "13319166247271113",
            "id": "10",
            "meta_info": {
               "last_visited_desktop": "13319166247271383"
            },
            "name": "DuckDuckGo",
            "type": "url",
            "url": "https://duckduckgo.com/"
         } ],
         "date_added": "13319166157194783",
         "date_modified": "13319166277591147",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13319166157194784",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}
)
return


User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: Extract pair of values from a json object.

Post by Datapoint » 28 Jan 2023, 23:38

Chunjee wrote:
26 Jan 2023, 15:47
what pair of values you wanna extract?
I guess it was this:
KilliK wrote:
26 Jan 2023, 06:14
how to create the pairs url-title-folder_name in their corresponding children groups?

Also, I didn't mention it above so FYI for anyone interested; the Jxon function I used above is from Coco's viewtopic.php?t=627. I assumed it was the function KilliK was using.

Post Reply

Return to “Ask for Help (v1)”