Jump to content
jesus40

Get data from json

Recommended Posts

jesus40
Hello friends, I need help getting data from json.

I read json from url into a variable.

Is possible to get volume_USD value etc.  where product_id= "ETH-BTC", ? So I want get "37421.69109185" as variable.

I think is better keeping it in the memory as variable. Or easier to convert it to db or csv?

 

 

#include <Inet.au3>
#include <json.au3>
$URL="https://api.abucoins.com/products/stats"
$data = _INetGetSource($URL)
MsgBox("","",$data)

$object=json_decode($data)

local $i=0

while 1

$message=json_get($object,'[product_id]');????????????????????????????????
MsgBox("","volume_USD",$message)

$i=$i+1
WEnd

 

the json structure is here:

https://api.abucoins.com/products/stats

[
  {
    "product_id": "ETH-BTC",
    "last": "0.05202015",
    "open": "0.05009375",
    "high": "0.05250000",
    "low": "0.04872420",
    "volume": "47.77440456",
    "volume_BTC": 2.48523169,
    "volume_USD": 37421.69109185,
    "volume_7d": "364.40240341",
    "volume_30d": "3555.83377072",
    "change": "3.85"
  },
  {
    "product_id": "LTC-BTC",
    "last": "0.01737490",
    "open": "0.01744636",
    "high": "0.01779000",
    "low": "0.01658091",
    "volume": "67.62762830",
    "volume_BTC": 1.17502328,
    "volume_USD": 17941.95920676,
    "volume_7d": "611.57893068",
    "volume_30d": "3881.57766044",
    "change": "-0.41"
  },
  {
    "product_id": "ETC-BTC",
    "last": "0.00191846",
    "open": "0.00189001",
    "high": "0.00198000",
    "low": "0.00162741",
    "volume": "66.48327742",
    "volume_BTC": 0.12754551,
    "volume_USD": 1947.54976323,
    "volume_7d": "738.94495089",
    "volume_30d": "4090.74712156",
    "change": "1.51"
  },
  {
    "product_id": "ZEC-BTC",
    "last": "0.03466163",
    "open": "0.03365452",
    "high": "0.03466163",
    "low": "0.03295811",
    "volume": "33.40101560",
    "volume_BTC": 1.15773364,
    "volume_USD": 17677.95599574,
    "volume_7d": "191.15953471",
    "volume_30d": "887.70158297",
    "change": "2.99"
  }
  ]

 

 

  • Like 1

Share this post


Link to post
Share on other sites
Jos

This should be close: :) 

#include <Inet.au3>
#include <json.au3>
$URL = "https://api.abucoins.com/products/stats"
$data = _INetGetSource($URL)
$object = json_decode($data)
Local $i = 0

While 1
    $product_id = json_get($object, '[' & $i & '].product_id')
    If @error Then ExitLoop
    If $product_id = "ETH-BTC" Then
        $volume_USD = json_get($object, '[' & $i & '].volume_USD')
        ConsoleWrite('$volume_USD = ' & $volume_USD & @CRLF ) ;### Debug Console
    EndIf
    $i += 1
WEnd

Jos

  • Like 4

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jesus40

thanks master, it worx like a charm :)

+++++

  • Like 1

Share this post


Link to post
Share on other sites
jesus40
On 2017. 12. 29. at 5:08 PM, Jos said:

This should be close: :) 

 

Jos

 

Hello, I have another one json. I tried with the same method but its not working.

I tried convert it to csv, but without success. It is different type, or I dont know.. Have You idea how to get the value of "free" where asset is "BTC"?

 

thank You

jesus40

{"makerCommission":10,"takerCommission":10,"buyerCommission":0,"sellerCommission":0,"canTrade":true,"canWithdraw":true,"canDeposit":true,"updateTime":1513881965965,"balances":[{"asset":"BTC","free":"0.00000011","locked":"0.00000000"},{"asset":"LTC","free":"0.00000022","locked":"0.00000000"},{"asset":"ETH","free":"0.00000000","locked":"0.00000000"},{"asset":"BNC","free":"0.00000000","locked":"0.00000000"},{"asset":"ICO","free":"0.00000000","locked":"0.00000000"},{"asset":"NEO","free":"0.00000000","locked":"0.00000000"},{"asset":"BNB","free":"0.00000000","locked":"0.00000000"},{"asset":"123","free":"0.00000000","locked":"0.00000000"},{"asset":"456","free":"0.00000000","locked":"0.00000000"},{"asset":"QTUM","free":"0.00000000","locked":"0.00000000"},{"asset":"EOS","free":"0.00000000","locked":"0.00000000"},{"asset":"SNT","free":"0.00000000","locked":"0.00000000"},{"asset":"BNT","free":"0.00000000","locked":"0.00000000"},{"asset":"GAS","free":"0.00000000","locked":"0.00000000"},{"asset":"BCC","free":"0.00000000","locked":"0.00000000"},{"asset":"BTM","free":"0.00000000","locked":"0.00000000"},{"asset":"USDT","free":"0.00000000","locked":"0.00000000"},{"asset":"HCC","free":"0.00000000","locked":"0.00000000"},{"asset":"HSR","free":"0.00000000","locked":"0.00000000"},{"asset":"OAX","free":"0.00000000","locked":"0.00000000"},{"asset":"DNT","free":"0.00000000","locked":"0.00000000"},{"asset":"MCO","free":"0.00000000","locked":"0.00000000"},{"asset":"ICN","free":"0.00000000","locked":"0.00000000"},{"asset":"ELC","free":"0.00000000","locked":"0.00000000"},{"asset":"PAY","free":"0.00000000","locked":"0.00000000"},{"asset":"ZRX","free":"0.00000000","locked":"0.00000000"},{"asset":"OMG","free":"0.00000000","locked":"0.00000000"},{"asset":"WTC","free":"0.00000000","locked":"0.00000000"},{"asset":"LRX","free":"0.00000000","locked":"0.00000000"},{"asset":"YOYO","free":"0.00000000","locked":"0.00000000"},{"asset":"LRC","free":"0.00000000","locked":"0.00000000"},{"asset":"LLT","free":"0.00000000","locked":"0.00000000"},{"asset":"TRX","free":"0.00000000","locked":"0.00000000"},{"asset":"FID","free":"0.00000000","locked":"0.00000000"},{"asset":"SNGLS","free":"0.00000000","locked":"0.00000000"},{"asset":"STRAT","free":"0.00000000","locked":"0.00000000"},{"asset":"BQX","free":"0.00000000","locked":"0.00000000"},{"asset":"FUN","free":"0.00000000","locked":"0.00000000"},{"asset":"KNC","free":"0.00000000","locked":"0.00000000"},{"asset":"CDT","free":"0.00000000","locked":"0.00000000"},{"asset":"XVG","free":"0.00000000","locked":"0.00000000"},{"asset":"IOTA","free":"0.00000000","locked":"0.00000000"},{"asset":"SNM","free":"0.00000000","locked":"0.00000000"},{"asset":"LINK","free":"0.00000000","locked":"0.00000000"},{"asset":"CVC","free":"0.00000000","locked":"0.00000000"},{"asset":"TNT","free":"0.00000000","locked":"0.00000000"},{"asset":"REP","free":"0.00000000","locked":"0.00000000"},{"asset":"CTR","free":"0.00000000","locked":"0.00000000"},{"asset":"MDA","free":"0.00000000","locked":"0.00000000"},{"asset":"MTL","free":"0.00000000","locked":"0.00000000"},{"asset":"SALT","free":"0.00000000","locked":"0.00000000"},{"asset":"NULS","free":"0.00000000","locked":"0.00000000"},{"asset":"SUB","free":"0.00000000","locked":"0.00000000"},{"asset":"STX","free":"0.00000000","locked":"0.00000000"},{"asset":"MTH","free":"0.00000000","locked":"0.00000000"},{"asset":"CAT","free":"0.00000000","locked":"0.00000000"},{"asset":"ADX","free":"0.00000000","locked":"0.00000000"},{"asset":"PIX","free":"0.00000000","locked":"0.00000000"},{"asset":"ETC","free":"0.00000000","locked":"0.00000000"},{"asset":"ENG","free":"0.00000000","locked":"0.00000000"},{"asset":"ZEC","free":"0.00000000","locked":"0.00000000"},{"asset":"AST","free":"0.00000000","locked":"0.00000000"},{"asset":"1ST","free":"0.00000000","locked":"0.00000000"},{"asset":"GNT","free":"0.00000000","locked":"0.00000000"},{"asset":"DGD","free":"0.00000000","locked":"0.00000000"},{"asset":"BAT","free":"0.00000000","locked":"0.00000000"},{"asset":"DASH","free":"0.00000000","locked":"0.00000000"},{"asset":"POWR","free":"0.00000000","locked":"0.00000000"},{"asset":"BTG","free":"0.00000000","locked":"0.00000000"},{"asset":"REQ","free":"0.00000000","locked":"0.00000000"},{"asset":"XMR","free":"0.00000000","locked":"0.00000000"},{"asset":"EVX","free":"0.00000000","locked":"0.00000000"},{"asset":"VIB","free":"0.00000000","locked":"0.00000000"},{"asset":"ENJ","free":"0.00000000","locked":"0.00000000"},{"asset":"VEN","free":"0.00000000","locked":"0.00000000"},{"asset":"CAG","free":"0.00000000","locked":"0.00000000"},{"asset":"EDG","free":"0.00000000","locked":"0.00000000"},{"asset":"ARK","free":"0.00000000","locked":"0.00000000"},{"asset":"XRP","free":"0.00000000","locked":"0.00000000"},{"asset":"MOD","free":"0.00000000","locked":"0.00000000"},{"asset":"AVT","free":"0.00000000","locked":"0.00000000"},{"asset":"STORJ","free":"0.00000000","locked":"0.00000000"},{"asset":"KMD","free":"0.00000000","locked":"0.00000000"},{"asset":"RCN","free":"0.00000000","locked":"0.00000000"},{"asset":"EDO","free":"0.00000000","locked":"0.00000000"},{"asset":"QASH","free":"0.00000000","locked":"0.00000000"},{"asset":"SAN","free":"0.00000000","locked":"0.00000000"},{"asset":"DATA","free":"0.00000000","locked":"0.00000000"},{"asset":"DLT","free":"0.00000000","locked":"0.00000000"},{"asset":"GUP","free":"0.00000000","locked":"0.00000000"},{"asset":"MCAP","free":"0.00000000","locked":"0.00000000"},{"asset":"MANA","free":"0.00000000","locked":"0.00000000"},{"asset":"PPT","free":"0.00000000","locked":"0.00000000"},{"asset":"OTN","free":"0.00000000","locked":"0.00000000"},{"asset":"CFD","free":"0.00000000","locked":"0.00000000"},{"asset":"RDN","free":"0.00000000","locked":"0.00000000"},{"asset":"GXS","free":"0.00000000","locked":"0.00000000"},{"asset":"AMB","free":"0.00000000","locked":"0.00000000"},{"asset":"ARN","free":"0.00000000","locked":"0.00000000"},{"asset":"BCPT","free":"0.00000000","locked":"0.00000000"},{"asset":"CND","free":"0.00000000","locked":"0.00000000"},{"asset":"GVT","free":"0.00000000","locked":"0.00000000"},{"asset":"POE","free":"0.00000000","locked":"0.00000000"},{"asset":"ALIS","free":"0.00000000","locked":"0.00000000"},{"asset":"BTS","free":"0.00000000","locked":"0.00000000"},{"asset":"FUEL","free":"0.00000000","locked":"0.00000000"},{"asset":"XZC","free":"0.00000000","locked":"0.00000000"},{"asset":"QSP","free":"0.00000000","locked":"0.00000000"},{"asset":"LSK","free":"0.00000000","locked":"0.00000000"},{"asset":"BCD","free":"0.00000000","locked":"0.00000000"},{"asset":"TNB","free":"0.00000000","locked":"0.00000000"},{"asset":"GRX","free":"0.00000000","locked":"0.00000000"},{"asset":"STAR","free":"0.00000000","locked":"0.00000000"},{"asset":"ADA","free":"0.00000000","locked":"0.00000000"},{"asset":"LEND","free":"0.00000000","locked":"0.00000000"},{"asset":"IFT","free":"0.00000000","locked":"0.00000000"},{"asset":"KICK","free":"0.00000000","locked":"0.00000000"},{"asset":"UKG","free":"0.00000000","locked":"0.00000000"},{"asset":"VOISE","free":"0.00000000","locked":"0.00000000"},{"asset":"XLM","free":"0.00000000","locked":"0.00000000"},{"asset":"CMT","free":"0.00000000","locked":"0.00000000"},{"asset":"WAVES","free":"0.00000000","locked":"0.00000000"},{"asset":"WABI","free":"0.00000000","locked":"0.00000000"},{"asset":"SBTC","free":"0.00000000","locked":"0.00000000"},{"asset":"BCX","free":"0.00000000","locked":"0.00000000"},{"asset":"GTO","free":"0.00000000","locked":"0.00000000"},{"asset":"ETF","free":"0.00000000","locked":"0.00000000"},{"asset":"ICX","free":"0.00000000","locked":"0.00000000"},{"asset":"OST","free":"0.00000000","locked":"0.00000000"},{"asset":"ELF","free":"0.00000000","locked":"0.00000000"},{"asset":"AION","free":"0.00000000","locked":"0.00000000"},{"asset":"WINGS","free":"0.00000000","locked":"0.00000000"},{"asset":"BRD","free":"0.00000000","locked":"0.00000000"},{"asset":"NEBL","free":"0.00000000","locked":"0.00000000"},{"asset":"NAV","free":"1.64000000","locked":"0.00000000"},{"asset":"VIBE","free":"0.00000000","locked":"0.00000000"},{"asset":"LUN","free":"0.00000000","locked":"0.00000000"},{"asset":"TRIG","free":"0.00774000","locked":"0.00000000"},{"asset":"APPC","free":"0.00000000","locked":"0.00000000"},{"asset":"CHAT","free":"0.00000000","locked":"0.00000000"}]}

 

Share this post


Link to post
Share on other sites
Jos

Guess we can start a masterclass JSON here too. :)

Try this one: (demo_json2.txt contains your json data)

#include <Inet.au3>
#include <json.au3>
$data = FileRead("demo_json2.txt")
$object = json_decode($data)
$balances = json_get($object, '.balances')
Local $i = 0

While 1
    $asset = json_get($object, '.balances[' & $i & '].asset')
    If @error Then ExitLoop
    if $asset = "BTC" Then
        $volume_USD = json_get($object, '.balances[' & $i & '].free')
        ConsoleWrite('! $asset:' & $asset & ' $volume_USD = ' & $volume_USD & @CRLF) ;### Debug Console
    EndIf
    $i += 1
WEnd

Jos

Edited by Jos
  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jesus40
39 minutes ago, Jos said:

Guess we can start a masterclass JSON here too. :)

Try this one: (demo_json2.txt contains your json data)

#include <Inet.au3>
#include <json.au3>
$data = FileRead("demo_json2.txt")
$object = json_decode($data)
$balances = json_get($object, '.balances')
Local $i = 0

While 1
    $asset = json_get($object, '.balances[' & $i & '].asset')
    If @error Then ExitLoop
    if $asset = "BTC" Then
        $volume_USD = json_get($object, '.balances[' & $i & '].free')
        ConsoleWrite('! $asset:' & $asset & ' $volume_USD = ' & $volume_USD & @CRLF) ;### Debug Console
    EndIf
    $i += 1
WEnd

Jos

 

I deleted the strings before "balances", and  looks like it worked, but Yours is much correct, thanks :)

Share this post


Link to post
Share on other sites
Jos
5 hours ago, jesus40 said:

 

I deleted the strings before "balances", and  looks like it worked, but Yours is much correct, thanks :)

Unsure what you mean by the deleted comment. The posted code works fine on the posted Data, so should work for you too....right?

I am working on a script that will help to code these JSON queries by making a JSON.DUMP() function which shows the proper keys.

Jos

  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jesus40

yes, worx fine.

What I tell, i deleted this from json file, except "balances" (before You replied)

{"makerCommission":10,"takerCommission":10,"buyerCommission":0,"sellerCommission":0,"canTrade":true,"canWithdraw":true,"canDeposit":true,"updateTime":1513881965965,"balances":

Share this post


Link to post
Share on other sites
Jos

Ok... understood.

Try out  this Json.Dump() UDF to see whether that is helpful to determine the proper Json path.
Simply feed it with the JSON string : Json_Dump($data) and run it with SciTE. It will dump the JSON in the SciTEOutput pane.

Jos

see here  

Edited by Jos
  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jesus40

thanks Jos, it worx. 

! Error JSONGET() different!  means?

I tried it with this:

 

$URL="https://api.binance.com/api/v1/exchangeInfo"
$data = _INetGetSource($URL)

 

Edited by jesus40

Share this post


Link to post
Share on other sites
Jos

The error means I made an mistake in the logic. There is a check build in to verify the found path and its value, so when that error is shown it means there is something wrong.... which is clearly the case here. ;)

Updated the logic and seems to be better (and simpler).

see here

Just give it a try and let me know if it now runs without that error.

Jos

Edited by Jos
  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jos

One other update to fix an bug in case the JSON data starts with an Array:

$debug = 0
#include <Inet.au3>
#include <json.au3>
$data = FileRead("demo_json4.htm")
Global $ObjLevel = -1
;~ Global $ObjPath = ""
Global $object = Json_Decode($data)
Json_Dump($data)
;[0].id

Func Json_Dump($Json, $InitTokenCount = 1000)
    Static $Jsmn_Init = __Jsmn_RuntimeLoader("jsmn_init"), $Jsmn_Parse = __Jsmn_RuntimeLoader("jsmn_parse")
    If $Json = "" Then $Json = '""'
    Local $TokenList, $Ret
    Local $Parser = DllStructCreate("uint pos;int toknext;int toksuper")
    Do
        DllCallAddress("none:cdecl", $Jsmn_Init, "ptr", DllStructGetPtr($Parser))
        $TokenList = DllStructCreate("byte[" & ($InitTokenCount * 20) & "]")
        $Ret = DllCallAddress("int:cdecl", $Jsmn_Parse, "ptr", DllStructGetPtr($Parser), "wstr", $Json, "ptr", DllStructGetPtr($TokenList), "uint", $InitTokenCount)
        $InitTokenCount *= 2
    Until $Ret[0] <> $JSMN_ERROR_NOMEM

    Local $Next = 0
    _Json_TokenDump($Json, DllStructGetPtr($TokenList), $Next)
EndFunc   ;==>Json_Dump

Func _Json_TokenDump(ByRef $Json, $Ptr, ByRef $Next, $ObjPath = "")
    If $Next = -1 Then Return Null

    Local $Token = DllStructCreate("int;int;int;int", $Ptr + ($Next * 20))
    Local $Type = DllStructGetData($Token, 1)
    Local $Start = DllStructGetData($Token, 2)
    Local $End = DllStructGetData($Token, 3)
    Local $Size = DllStructGetData($Token, 4)
    $Next += 1

    If $Type = 0 And $Start = 0 And $End = 0 And $Size = 0 Then ; Null Item
        $Next = -1
        Return Null
    EndIf

    Switch $Type
        Case 0 ; Json_PRIMITIVE
            Local $Primitive = StringMid($Json, $Start + 1, $End - $Start)
            Switch $Primitive
                Case "true"
                    Return True
                Case "false"
                    Return False
                Case "null"
                    Return Null
                Case Else
                    If StringRegExp($Primitive, "^[+\-0-9]") Then
                        Return Number($Primitive)
                    Else
                        Return Json_StringDecode($Primitive)
                    EndIf
            EndSwitch

        Case 1 ; Json_OBJECT
            $ObjLevel += 1
            If $debug Then ConsoleWrite('###>- Start objectlevel=' & $ObjLevel & " $Next=" & $Next & " $ObjPath:" & $ObjPath & "  Size:" & $Size & @CRLF) ;### Debug Console
            For $i = 0 To $Size - 1 Step 2
                Local $Key = _Json_TokenDump($Json, $Ptr, $Next)
                $cObjPath = $ObjPath & "." & $Key
                If $debug Then ConsoleWrite('+>- Start objectlevel=' & $ObjLevel & " $Next=" & $Next & " $cObjPath:" & $cObjPath & "  Size:" & $Size & @CRLF) ;### Debug Console
                Local $Value = _Json_TokenDump($Json, $Ptr, $Next, $ObjPath & "." & $Key)
                If $debug Then ConsoleWrite('-<- Return objectlevel=' & $ObjLevel & " $Next=" & $Next & " $cObjPath:" & $ObjPath & "  Size:" & $Size & @CRLF) ;### Debug Console
                If Not ($Value = False) Then
                    If Not IsString($Key) Then
                        $Key = Json_Encode($Key)
                    EndIf
                    If $ObjLevel <= 0 Then
                        $CurJSONKey = "." & $Key
                    EndIf
                    ; show the key and its value
                    ConsoleWrite("+-> " & $cObjPath & '  =' & $Value & @CRLF)
                    If json_get($object, $cObjPath) <> $Value Then
                        ConsoleWrite("! Error JSONGET() different!  " & $cObjPath & '=' & $Value & '   JSONGET("' & $cObjPath & '")=' & json_get($object, $cObjPath) & @CRLF)
                    EndIf
                EndIf
            Next
            If $debug Then ConsoleWrite('###< End object ' & $Next & @CRLF) ;### Debug Console
            $ObjLevel -= 1
            Return False
        Case 2 ; Json_ARRAY
            Local $sObjPath = $ObjPath
            If $debug Then ConsoleWrite('$$$> Start Json_ARRAY:' & $ObjPath & "  Size:" & $Size & @CRLF) ;### Debug Console
            Local $Array[$Size]
            For $i = 0 To $Size - 1
                $sObjPath = $ObjPath & "[" & $i & "]"
                If $debug Then ConsoleWrite('$$$> TokenDump _ARRAY:' & $sObjPath & "  Size:" & $Size & @CRLF) ;### Debug Console
                $Value = _Json_TokenDump($Json, $Ptr, $Next, $sObjPath)
                If $Value <> False Then
                    ; show the key and its value
                    ConsoleWrite("+=> " & $sObjPath & "=>" & $Value & @CRLF)
                EndIf
            Next
            If $debug Then ConsoleWrite('$$$< End   Json_ARRAY:' & $ObjPath & @CRLF) ;### Debug Console
            $ObjPath = $sObjPath
            Return False

        Case 3 ; Json_STRING
            Local $LastKey = Json_StringDecode(StringMid($Json, $Start + 1, $End - $Start))
            If $debug Then ConsoleWrite("=>" & $ObjPath & "." & $LastKey & '  (JSON STRING)' & @CRLF)
            Return $LastKey
    EndSwitch
EndFunc   ;==>_Json_TokenDump

Jos

  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jesus40

thx Jos,  it is working like a charm. It really helped me.

Edited by jesus40

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Ward
      By Ward
      Introduction
      JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual).

      I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example.
       
      Important Update!!
      I rename the library from jsmn.au3 to json.au3. All function names are changed, too.
       
      Decoding Function
      Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts:
      { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used.

      If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART.

      Encoding Function
      Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null.

      $Option is bitmask consisting following constant:
      $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode().

      $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING.

      Get and Put Functions
      Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example:
      Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions
      Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it.
       
      == Update 2013/05/19 ==
       
      * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001).
      $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f)  
      == Update 2015/01/08 ==
      * Rename the library from jsmn.au3 to json.au3. All function names are changed, too.
      * Add Json_Put() and Json_Get()
      * Add Null support
      * Using BinaryCall.au3 to loading the machine code.
       
      == Update 2018/01/13==   (Jos)
      * Add JsonDump() to list all Json Keys and their values to easily figure out what they are.
      == Update 2018/10/01==  (Jos)
      * Fixed JsonDump() some fields and values were not showing as discussed here  - tnx @TheXman .
      == Update 2018/10/01b==   (Jos)
      * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman .

      Json(2018.10.01b).zip  
    • satanico64
      By satanico64
      hi guys !
      how are you ? fine ? yeahhh
      Here is my problem:
      I have 2 autoit applications, a.exe and b.exe
      In a.exe, i call b.exe with a json as parameter:
      FileWrite("N:\+++ Dev\ClientServeur\FolderMonitor.nca", "lejson in A:" & Json_Encode($json_tmp)& @CRLF) Run(@ScriptDir & "\FolderMonitor.exe " & Json_Encode($json_tmp) )                        
              In program B:
      ;Firstline of program: FileWrite("N:\+++ Dev\ClientServeur\FolderMonitor.nca", @CRLF & "json in B:" & $CmdLine[1] & @CRLF)        
              Output:
                     
              lejson in A:{"DOSSIERS":{"Dossier_Hidden":"N:\\+++ Dev\\ClientServeur\\","Dossier_du_script":"N:\\+++ Dev\\ClientServeur\\","Fichier_Historique":"N:\\+++ Dev\\ClientServeur\\Historique.log","Fichier_DEBUG":"N:\\+++ Dev\\ClientServeur\\FolderMonitor.nca"}}
              json in B  :{DOSSIERS:{Dossier_Hidden:N:\\+++ Dev\\ClientServeur\\,Dossier_du_script:N:\\+++ Dev\\ClientServeur\\,Fichier_Historique:N:\\+++ Dev\\ClientServeur\\Historique.log,Fichier_DEBUG:N:\\+++ Dev\\ClientServeur\\FolderMonitor.nca}}
      As you see, i lost my quote in my json so it's au problem to parse my json.
      I use THIS library
      I tried different type of encoding (ex JSON_STRICT_PRINT), but i don't think that it's the problem. Maybe i'm wrong
       
      thanks
      Nicolas.
    • jandings
      By jandings
      Hello there,
      since I spent some time to access the REST-API of the web shop system Shopware, I'd like to share a few lines to make life easier for others.
      With this you can access your Shopware database, reading and writing all kind of data. 
      Replies are JSON style.
      To work with JSON I usually depend on either just own AutoIt string operations or this AutoIt library:
      ; File        : Json.au3 (2015.01.08)
      ; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
      ; Author    : Ward
      $UserName="xxxx" ;Shopware credentials of Shopware user who has the API checkbox ticked $PassWord="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;API-Key of this user, NOT the Password $URL="www.XXX.com/api/" $Command="orders/64682?useNumberAsId=true" ;A list of commands and options is available through shopware REST-API help; Here we read order number 64682 $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://" & $URL & $Command , False) $oHTTP.SetCredentials($UserName, $PassWord, 0) $oHTTP.Send() $response = $oHTTP.ResponseText ConsoleWrite ($response & @CRLF & @CRLF)  
    • BJP
      By BJP
      Hello.  I have a program that has used ADO database connection to return a query and then subsequently put the query results into an array using getrows.  See snippet below:
      $constrim="DRIVER={SQL Server};SERVER=xxx-xxxxx\CSC;DATABASE=xxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxxxx;" $adCN = ObjCreate ("ADODB.Connection") ; <== Create SQL connection $adCN.Open ($constrim) ; local $sQuery = "select * from tbl_Apps"                     ; get all applications in the database local $oAppRecordSet = $adCN.Execute($sQuery) local $aAppsInDB = $oAppRecordSet.Getrows(5000) With the code above I can perform array operations very efficiently. 
      Now I need to get this data via JSON which is working (using Ward's JSON UDF)  but the data set returned is large (approx 4MB) and I'm wondering what is the most efficient way to get this data into an array.  
      Dim $obj = ObjCreate ("WinHttp.WinHttpRequest.5.1") $obj.Open("GET", $URL, false) $obj.SetRequestHeader("Content-Type", "application/json") $obj.Send() $json = JSON_decode( $obj.ResponseText ) Any help would be appreciated!!
    • baolo073
      By baolo073
      [ [ [ "Kinh Oanh,\r\n", "Dear Oanh,\r\n", null, null, 3 ], [ "C\u1ea3m \u01a1n b\u1ea1n r\u1ea5t nhi\u1ec1u v졢\u1ee9c th\u01b0 c\u1ee7a b\u1ea1n \u0111\u1ebfn m\u1ed9t v᩠ngṠtr\u01b0\u1edbc. ", "Thank you very much for your letter which arrived a few days ago.", null, null, 3 ], [ "Th\u1eadt \u0111⯧ y뵠khi nghe t\u1eeb b\u1ea1n.\r\n", "It was lovely to hear from you.\r\n", null, null, 3 ], [ "b\u1ea1n \u0111i \u0111㵠v\u1eady.\r\n", "where do you go.\r\n", null, null, 1 ], [ "T\u1ea1m bi\u1ec7t!", "Goodbye!", null, null, 1 ] ], null, "en", null, null, null, 0.91366601, null, [ [ "en" ], null, [ 0.91366601 ], [ "en" ] ] ] How to parse array to json?
×