JSON Elements to Objects

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

JSON Elements to Objects

13 May 2021, 23:01

I have tried for HOURS trying to get this JSON element to filter out in for loop, but I just can't get it. It works when I Hard Code the edited array into ahk, but as soon as I try and grab it and filter out key/value pairs, it just keeps giving blank or no msgbox.

I have Json.ahk included.

Anybody know the fix for this?

Code: Select all

{"type":"voi","voi":{"year":2021,"make":"Chevrolet","model":"Corvette","vin":"1G1YC2D47M5105400","stockType":"Used","stockNumber":"M5105400P","trim":"Stingray Premium Cpe w/ 3LT"}}
https://www.autotrader.com/cars-for-sal ... pe=listing

base_info=document.querySelectorAll("div[data-cmp*='ownerCTAs']")[0].children[0].attributes[2].nodeValue

Code: Select all

Trim_Make_Mod:=page.Evaluate(base_info).value
MsgBox % Trim_Make_Mod
Trim_Make_Mod:=RegExReplace(Trim_Make_Mod, "\[","")
Trim_Make_Mod:=RegExReplace(Trim_Make_Mod, "\]","")
MsgBox % Trim_Make_Mod
;~ GuiControl,, guiEdit28, %Trim_Make_Mod%
;~ Clipboard:=Trim_Make_Mod
if InStr(Trim_Make_Mod,"voi")
	{
		MsgBox Voi InStr
		Trim_Make_Mod:=JSON.Load(Trim_Make_Mod)
		;~ Trim_Make_Mod=%Trim_Make_Mod%
	}
asset:=Trim_Make_Mod.voi
for k,v in asset
	MsgBox % k " : " v
User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: JSON Elements to Objects

13 May 2021, 23:58

You could try downloading it to the HDD then fileread should work.

Code: Select all

FileRead, OutputVar, % A_ScriptDir "\car.json"
obj := JSON.parse(OutputVar)
msgbox, % obj["voi"].make
; => Chevrolet
msgbox, % obj["voi"].model
; => Corvette
msgbox, % obj["voi"].trim
; => Stingray Premium Cpe w/ 3LT

As for looping, looks like the trim is the 5th item if you must loop.

Code: Select all

for key, value in obj["voi"] {
	if (key == "trim" || A_Index == 5) {
		trim := value
	}
}
msgbox, % trim
; => Stingray Premium Cpe w/ 3LT
Last edited by Chunjee on 14 May 2021, 00:03, edited 1 time in total.
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:03

I wonder if parse would work from the element value.

I'll let you know after I finish warming up my chili...
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:07

JSON.parse did NOT work...

I've really got my heart set on not putting it in notepad...
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:08

I went the regexmatch route and there became too many variations once I go to trim...
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:11

There's a way to do it in javascript... but there's a learning curve I'm not sure I'm willing to traverse tonight lol
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:19

Do you this the "/" in the trim is tripping it up?
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: JSON Elements to Objects

14 May 2021, 00:22

I removed the forward slash and no change
User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: JSON Elements to Objects

14 May 2021, 00:34

HeXaDeCiMaToR wrote:
14 May 2021, 00:07
JSON.parse did NOT work...
Sorry that should probably be JSON.Load for your version of Json.ahk

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1 and 146 guests