JSON -> AHK Obj : How to show obj details? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rc76
Posts: 147
Joined: 07 Nov 2020, 01:45

JSON -> AHK Obj : How to show obj details?

07 Nov 2021, 05:32

I am using coco's JSON.ahk file to convert a json output into an AHK obj:

Code: Select all

BNB_Data := json.load(json_response)
And the json output example with specific outline/structure is as follow:

Code: Select all

{
   "assets": [
      {
         "asset": "BTC",
         "availableBalance": "0.00000000",
         "crossUnPnl": "0.00000000",
         "crossWalletBalance": "0.00000000",
         "initialMargin": "0.00000000",
         "maintMargin": "0.00000000",
         "marginAvailable": 1,
         "marginBalance": "0.00000000",
         "maxWithdrawAmount": "0.00000000",
         "openOrderInitialMargin": "0.00000000",
         "positionInitialMargin": "0.00000000",
         "unrealizedProfit": "0.00000000",
         "updateTime": 0,
         "walletBalance": "0.00000000"
      },
      {
         "asset": "BUSD",
         "availableBalance": "10.00000000",
         "crossUnPnl": "0.00000000",
         "crossWalletBalance": "10.00000000",
         "initialMargin": "0.00000000",
         "maintMargin": "0.00000000",
         "marginAvailable": 1,
         "marginBalance": "10.00000000",
         "maxWithdrawAmount": "10.00000000",
         "openOrderInitialMargin": "0.00000000",
         "positionInitialMargin": "0.00000000",
         "unrealizedProfit": "0.00000000",
         "updateTime": 1630130417327,
         "walletBalance": "10.00000000"
      }
   ],
   "availableBalance": "0.00000000",
   "canDeposit": 1,
   "canTrade": 1,
   "canWithdraw": 1,
   "feeTier": 0,
   "maxWithdrawAmount": "0.00000000",
   "positions": [
      {
         "entryPrice": "0.0",
         "initialMargin": "0",
         "isolated": 0,
         "isolatedWallet": "0",
         "leverage": "20",
         "maintMargin": "0",
         "maxNotional": "25000",
         "notional": "0",
         "openOrderInitialMargin": "0",
         "positionAmt": "0.0",
         "positionInitialMargin": "0",
         "positionSide": "BOTH",
         "symbol": "RAYUSDT",
         "unrealizedProfit": "0.00000000",
         "updateTime": 0
      },
      {
         "entryPrice": "0.0",
         "initialMargin": "0",
         "isolated": 0,
         "isolatedWallet": "0",
         "leverage": "20",
         "maintMargin": "0",
         "maxNotional": "25000",
         "notional": "0",
         "openOrderInitialMargin": "0",
         "positionAmt": "0",
         "positionInitialMargin": "0",
         "positionSide": "BOTH",
         "symbol": "CTSIUSDT",
         "unrealizedProfit": "0.00000000",
         "updateTime": 0
      }
   ],
   "totalCrossUnPnl": "0.00000000",
   "totalCrossWalletBalance": "0.00000000",
   "totalInitialMargin": "0.00000000",
   "totalMaintMargin": "0.00000000",
   "totalMarginBalance": "0.00000000",
   "totalOpenOrderInitialMargin": "0.00000000",
   "totalPositionInitialMargin": "0.00000000",
   "totalUnrealizedProfit": "0.00000000",
   "totalWalletBalance": "0.00000000",
   "updateTime": 0
}


Question:

Q1:
If I want to output (i.e. using msgbox) all of the assets, how can I achieve that? I tried many approaches but just keep return nothing...

Code: Select all

msg := "assets: " BNB_Data["assets"]

msg := "assets: " BNB_Data.assets

msg := "assets: " BNB_Data[0]
Q2:
If I want to return all the relevant parameters for a particular asset (i.e. BTC), how can I do that? I tried quite a few approach and don't work as well..

Code: Select all

msg := "BTC: " BNB_Data["assets=BTC"]

msg := "assets: " BNB_Data.assets["BTC"]

msg := "assets: " BNB_Data.assets[0]
Q3
Is there any good tutorials about navigating/operating a JSON AHK object? I searched a bit, I can only find the ones about AHK object, and not really teaching much about how to navigate a "JSON" obj...
User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: JSON -> AHK Obj : How to show obj details?

07 Nov 2021, 06:35

Code: Select all

#Include d:\utils\JSON.ahk
FileRead, json_response, %A_ScriptDir%\test.json
BNB_Data := json.load(json_response), assets := ""
For each, asset in BNB_Data.assets ; BNB_Data.assets is an array of associative arrays
 assets .= "`n" asset.asset        ; Append the asset value for each array element
MsgBox, 64, Assets, % SubStr(assets, 2)
MsgBox, 64, BTC Available Balance, % assetVal(BNB_Data, "BTC", "availableBalance")

assetVal(json, asset, key) {
 For each, set in json.assets      ; set is one of the associative arrays
  If (set.asset = asset)           ; set.asset is the value of the "asset" key
   Return set[key]                 ; For that asset, return the value of the specified key
}
I would learn about :arrow: Arrays, because the object returned by JSON.ahk is an array.
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: JSON -> AHK Obj : How to show obj details?

07 Nov 2021, 11:32

When I was first getting started with objects and arrays I found Array_GUI very useful for visualizing and diving deep into arrays.

rc76 wrote:
07 Nov 2021, 05:32
Q2:
If I want to return all the relevant parameters for a particular asset (i.e. BTC), how can I do that? I tried quite a few approach and don't work as well..
I would recommend https://biga-ahk.github.io/biga.ahk/#/?id=find which will let you find the first object that matches the "asset" key with a value of "BTC" like so:

Code: Select all

btcDetails := A.find(bnb_Data.assets, {"asset": "BTC"})
; => [{"asset": "BTC", "availableBalance": "0.00000000", "crossUnPnl": "0.00000000", "crossWalletBalance": "0.00000000", "initialMargin": "0.00000000", "maintMargin": "0.00000000", "marginAvailable": 1, "marginBalance": "0.00000000", "maxWithdrawAmount": "0.00000000", "openOrderInitialMargin": "0.00000000", "positionInitialMargin": "0.00000000", "unrealizedProfit": "0.00000000", "updateTime": 0, "walletBalance": "0.00000000"}]
You don't have multiple "asset": "BTC" objects but if you did and wanted all of them; https://biga-ahk.github.io/biga.ahk/#/?id=filter would be more useful as .find will only return the first one encountered.


The vanilla ahk way to find them would be a for loop:

Code: Select all

for key, value in bnb_Data.assets {
	if (value.asset == "BTC") {
		; found it
	}
}
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: JSON -> AHK Obj : How to show obj details?

07 Nov 2021, 14:20

rc76 wrote:
07 Nov 2021, 05:32
Q1:
If I want to output (i.e. using msgbox) all of the assets, how can I achieve that? I tried many approaches but just keep return nothing...
msgbox cannot display objects. You can turn them into strings for use with msgbox if desired: msgbox, % JSON.stringify(BNB_Data["assets"]) or equivalent.


There is a useful function called print() that is used commonly on the ahk discord; it works similar to msgbox but will not stop the thread and works with objects:

Code: Select all

#Persistent
Print(obj, quote:=False, end:="`n")
{
    static _ := DllCall("AllocConsole"), cout := FileOpen("CONOUT$", "w")
    , escapes := [["``", "``" "``"], ["""", """"""], ["`b", "``b"]
    , ["`f", "``f"], ["`r", "``r"], ["`n", "``n"], ["`t", "``t"]]
    if IsObject(obj) {
        for k in obj
            is_array := k == A_Index
        until !is_array
        cout.Write(is_array ? "[" : "{")
        for k, v in obj {
            cout.Write(A_Index > 1 ? ", " : "")
            is_array ? _ : Print(k, 1, "") cout.Write(": ")
            Print(v, 1, "")
        } return cout.Write(( is_array ? "]" : "}") end), end ? cout.__Handle : _
    } if (!quote || ObjGetCapacity([obj], 1) == "")
        return cout.Write(obj . end), end ? cout.__Handle : _
    for k, v in escapes
        obj := StrReplace(obj, v[1], v[2])
    while RegExMatch(obj, "O)[^\x20-\x7e]", m)
        obj := StrReplace(obj, m[0], Format(""" Chr({:04d}) """, Ord(m[0])))
    return cout.Write("""" obj """" . end), end ? cout.__Handle : _
}
rc76
Posts: 147
Joined: 07 Nov 2020, 01:45

Re: JSON -> AHK Obj : How to show obj details?  Topic is solved

29 Nov 2021, 10:37

Thank you so much @mikeyww and @Chunjee !

These info are great! I have learned a great progress in dealing with JSON now! Thank you so much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, OrangeCat and 190 guests