Ward

A Non-Strict JSON UDF (JSMN)

66 posts in this topic




Hey,

thanks for the UDF, good work. But I have problems with a specific json source -> http://data.mtgox.com/api/1/BTCUSD/depth

I can't extract the values. First I used the other JSON UDF from Gabriel Boehme and I have exactly the same problems.

I get an empty array with ~500 elements when I get into the asks or bids arrays.

This is what I'm doing to get the data:

Func getArrayFromJson($URI,$reqAuth,$post="")
$result = Jsmn_Decode(goxRequest($URI,_Iif($reqAuth>0,getNonce()&$post,"")))
$result = Jsmn_ObjTo2DArray($result)
If (Not IsArray($result) Or UBound($result) = 0) Then
MsgBox(0,"Error: "&$URI,$result)
Return 0
Else
If(NOT IsArray($result[2][1])) Then
MsgBox(0,$URI,$result[2][1])
Return 0
EndIf
Return $result[2][1]
EndIf
EndFunc

It works on every other API without problems, just not with this one.

Share this post


Link to post
Share on other sites

Try this code, then your will see every element is decoded succeed.

#Include "JSMN.au3"

Local $Json = BinaryToString(InetRead("http://data.mtgox.com/api/1/BTCUSD/depth"), 4)
Local $Obj = Jsmn_Decode($Json)

ConsoleWrite(Jsmn_Encode($Obj, $JSMN_PRETTY_PRINT))

Then why you got empty output?

Because in your code, "$result[2][1]" is an array, so you can't use MsgBox to output it.


新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites

What for an impolite user!

Thanks it works very fine! Randomly, I need it for Mt. Gox Too ;)

Share this post


Link to post
Share on other sites

Hey Ward,

fist of all thanks for JSON extension! I appreciate the work you spent to help this community with your programming skills!

I am new to this forum and autoit is one of my personal interests so I would like to say "Hello everyone" :bye:.

Maybe someone can help me with my question?:

I want to query a JSON wheather service to get the actual wheater and for future use also the forecast.

For this I use OperWheatherMap API and build my JSON, e.g. for today in Berlin:

{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]}

My special interest lays in the params "temp":{"day":13.22,"min":10.57,"max":13.22 ... and  "description":"mäßiger Regen" but I am not able to extract this information using JSMN.

My code:

#include <JSMN.au3>

; Anfrage von Wetter mit JSON Antwort: http://api.openweathermap.org/data/2.5/forecast/daily?q=Berlin&mode=json&units=metric&cnt=1&lang=de
local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]}'

Local $objJson = Jsmn_Decode($Json1)
If (Jsmn_IsObject($objJson)) Then
    ConsoleWrite('Data1:' & Jsmn_ObjGetKeys($objJson) & @CRLF)
    ConsoleWrite('Data2:' & Jsmn_ObjGet($objJson, "message") & @CRLF)
    ConsoleWrite('Data3:' & Jsmn_ObjGetCount($objJson) & @CRLF)
    ConsoleWrite('Data4:' & Jsmn_ObjExists($objJson, "message") & @CRLF)
Else
    ConsoleWrite("Kein Objekt")
EndIf

The according console output:

Data1:
Data2:0.0268
Data3:5
Data4:True

So the first level was queried successfully but if I want to query the params mentioned above I get an empty result:

AutoIt:

#include <JSMN.au3>

; Anfrage von Wetter mit JSON Antwort: http://api.openweathermap.org/data/2.5/forecast/daily?q=Berlin&mode=json&units=metric&cnt=1&lang=de
local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]}'
Local $objJson = Jsmn_Decode($Json1)
If (Jsmn_IsObject($objJson)) Then
    ConsoleWrite('Data1:' & Jsmn_ObjGetKeys($objJson) & @CRLF)
    ConsoleWrite('Data2:' & Jsmn_ObjGet($objJson, "day") & @CRLF)
    ConsoleWrite('Data3:' & Jsmn_ObjGetCount($objJson) & @CRLF)
    ConsoleWrite('Data4:' & Jsmn_ObjExists($objJson, "day") & @CRLF)
Else
    ConsoleWrite("Kein Objekt")
EndIf
Local $objJson2 = Jsmn_Decode($objJson)

Console:

Data1:
Data2:
Data3:6
Data4:True

Because this is my first contact with JSON I think that I am doing something wrong with the the function "Jsmn_ObjGet". But what am I doing wrong?

Regards

mmscripting

Share this post


Link to post
Share on other sites

Try this:

#include <JSMN.au3>

; Anfrage von Wetter mit JSON Antwort: http://api.openweathermap.org/data/2.5/forecast/daily?q=Berlin&mode=json&units=metric&cnt=1&lang=de
local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]}'
Local $objJson = Jsmn_Decode($Json1)

Local $List = Jsmn_ObjGet($objJson, "list") ; "list" is an array
Local $objJson2 = $List[0]
Local $Temp = Jsmn_ObjGet($objJson2, "temp") ; "temp" is an object

ConsoleWrite(Jsmn_ObjGet($Temp, "day") & @LF)
ConsoleWrite(Jsmn_ObjGet($Temp, "min") & @LF)

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites

Hi Ward,

ahh ok now I understand: You have do know about the JSON structure and like an encapsulation take each part in an own list / array and at some point there is only a string left which can be queried for or values.

Before I thought that I can just query the whole JSON object and the function will give me the value in return. Is this also in other programming languages a common behaviour to "encapsulate" the whole JSON object or maybe is there a way to query the whole object for the needed string?

Anyway: Thanks for your very fast and efficient help :thumbsup: !

Regards

mmscripting

Share this post


Link to post
Share on other sites

Funny that I need such a UDF now. This has to be the best JSON UDF I've seen floating around the Forums. Thanks ward.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#10 ·  Posted

I wrote a helper function to get specific element in nested array/object returned from Jsmn_Decode() more easily.

Here is the code and example:

#Include "JSMN.au3"

Local $Json = '{"key1" : "value1", "key2" : [true, ["a", "b"], {"key3", "obj_in_array"}]}'
Local $Obj = Jsmn_Decode($Json)
Jsmn_Get_ShowResult($Obj, '["key1"]')
Jsmn_Get_ShowResult($Obj, '["key2"][0]')
Jsmn_Get_ShowResult($Obj, '["key2"][1][0]')
Jsmn_Get_ShowResult($Obj, '["key2"][2]["key3"]')

Local $Json = '{"strange key []" : "uses \\uXXXX for unsupported char in the key", "key_nospace_1" : {"key_nospace_2" : "don''t need \" if the key has no space"} }'
Local $Obj = Jsmn_Decode($Json)
Jsmn_Get_ShowResult($Obj, '["strange key [\u005D"]')
Jsmn_Get_ShowResult($Obj, '[key_nospace_1][key_nospace_2]')

Jsmn_Get_ShowResult($Obj, '[error1]')
Jsmn_Get_ShowResult($Obj, '[error2')


Func Jsmn_Get_ShowResult($Var, $Key)
    Local $Ret = Jsmn_Get($Var, $Key)
    If @Error Then
        Switch @error
            Case 1
                ConsoleWrite("Error 1: key not exists" & @LF)
            Case 2
                ConsoleWrite("Error 2: syntax error" & @LF)
        EndSwitch

    Else
        ConsoleWrite($Key & " => " & VarGetType($Ret) & ": " & $Ret & @LF)
    EndIf
EndFunc

Func Jsmn_Get($Var, $Key)
    If Not $Key Then Return $Var

    Local $Match = StringRegExp($Key, "(^\[([^\]]+)\])", 3)
    If IsArray($Match) Then
        Local $Index = Jsmn_Decode($Match[1])
        $Key = StringTrimLeft($Key, StringLen($Match[0]))

        If IsString($Index) And Jsmn_IsObject($Var) And Jsmn_ObjExists($Var, $Index) Then
            Local $Ret = Jsmn_Get(Jsmn_ObjGet($Var, $Index), $Key)
            Return SetError(@Error, 0, $Ret)

        ElseIf IsNumber($Index) And IsArray($Var) And $Index >= 0 And $Index < UBound($Var) Then
            Local $Ret = Jsmn_Get($Var[$Index], $Key)
            Return SetError(@Error, 0, $Ret)

        Else
            Return SetError(1, 0, "")

        EndIf
    EndIf

    Return SetError(2, 0, "")
EndFunc
2 people like this

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

I'm trying to parse the data below,

{"events":[{"world_id":2012,"map_id":873,"event_id":"659149D4-43EC-4DCB-A6BB-0B2D402B537B","state":"Warmup"},
{"world_id":2012,"map_id":873,"event_id":"72F93CD8-94AC-4234-8D86-996CCAC76A46","state":"Warmup"},
{"world_id":2012,"map_id":873,"event_id":"81F89AC2-4764-49CB-A4F1-8B7546201DC7","state":"Active"},
{"world_id":2012,"map_id":873,"event_id":"FF71DE90-423B-4685-A343-83487A330C7A","state":"Active"}]}

The end result would be an array with 3 columns for map_id, event_id, and state filled with the relevant data.

By striping '{"events":[' and ']}' from either end i managed to get the values of the first line/object but how do extract further items? I can't work out how to loop into the next line(s).

Thanks for any help. My code isn't really worth showing hence no code example.

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

Using Jsmn_Get(), it's easy.

I will put this function into the UDF later.

#Include "JSMN.au3"

Local $Json = '{"events":[{"world_id":2012,"map_id":873,"event_id":"659149D4-43EC-4DCB-A6BB-0B2D402B537B","state":"Warmup"},{"world_id":2012,"map_id":873,"event_id":"72F93CD8-94AC-4234-8D86-996CCAC76A46","state":"Warmup"},{"world_id":2012,"map_id":873,"event_id":"81F89AC2-4764-49CB-A4F1-8B7546201DC7","state":"Active"},{"world_id":2012,"map_id":873,"event_id":"FF71DE90-423B-4685-A343-83487A330C7A","state":"Active"}]}'
Local $Obj = Jsmn_Decode($Json)

ConsoleWrite(Jsmn_Get($Obj, '["events"][0]["world_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][0]["map_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][0]["event_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][0]["state"]') & @LF)

ConsoleWrite(Jsmn_Get($Obj, '["events"][1]["world_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][1]["map_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][1]["event_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][1]["state"]') & @LF)

ConsoleWrite(Jsmn_Get($Obj, '["events"][2]["world_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][2]["map_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][2]["event_id"]') & @LF)
ConsoleWrite(Jsmn_Get($Obj, '["events"][2]["state"]') & @LF)

Func Jsmn_Get($Var, $Key)
    If Not $Key Then Return $Var

    Local $Match = StringRegExp($Key, "(^\[([^\]]+)\])", 3)
    If IsArray($Match) Then
        Local $Index = Jsmn_Decode($Match[1])
        $Key = StringTrimLeft($Key, StringLen($Match[0]))

        If IsString($Index) And Jsmn_IsObject($Var) And Jsmn_ObjExists($Var, $Index) Then
            Local $Ret = Jsmn_Get(Jsmn_ObjGet($Var, $Index), $Key)
            Return SetError(@Error, 0, $Ret)

        ElseIf IsNumber($Index) And IsArray($Var) And $Index >= 0 And $Index < UBound($Var) Then
            Local $Ret = Jsmn_Get($Var[$Index], $Key)
            Return SetError(@Error, 0, $Ret)

        Else
            Return SetError(1, 0, "")

        EndIf
    EndIf

    Return SetError(2, 0, "")
EndFunc

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thanks! Wow, it's easy to do once you know the correct syntax. Thanks again for the UDF and your code above really helps me understand it more now :)

Edited by Fly By Night

Share this post


Link to post
Share on other sites

#14 ·  Posted

Perfect, right at the time i thought of buying the $400 n/Software json Activex the native and free udf comes around, thank you very much.


$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)

Share this post


Link to post
Share on other sites

#15 ·  Posted

Can it be possible to use put and get via JSON Dot Notation?

Share this post


Link to post
Share on other sites

#16 ·  Posted

I'm searching through a long (>800) list looking for a specific value using the following code and my routine seems to be very slow compared to using a similar version just using _StringBetween on the raw JSON string.

I'm not sure if i can reduce the number of Jsmn_Get calls using the logic below. Is the While..Wend method below one to use or am i missing a better way to hunt for a value. Thanks again.

Func getEventstate($eventID)


; Search for a specific EventID and return it's state


Local $evState = "", $evFound = 0
Local $i = 0

    While ((Jsmn_Get($EventsObj, '["events"][' & $i & ']["event_id"]') <> "") And $evFound = 0)

        If Jsmn_Get($EventsObj, '["events"][' & $i & ']["event_id"]') == $eventID Then
            $evState = Jsmn_Get($EventsObj, '["events"][' & $i & ']["state"]')
            $evFound = 1
        EndIf

        $i = $i + 1
    WEnd

    Return ($evState)
EndFunc   ;==>getEventstate2

For reference, same using _StringBetween

Func getEventstate($eventID)

    $srcStr = '"event_id":"' & $eventID & '","state":"'
    $evState = _StringBetween($EventsJson, $srcStr, '"}')

    If $evState <> "" Then
        Return ($evState[0])
    EndIf
EndFunc   ;==>getEventstate

Share this post


Link to post
Share on other sites

#17 ·  Posted

You can optimize this code (and maybe any code) to avoid do the same thing everytime in the loop. For example, you can get '["events"]' object and reuse it in the loop. And then you can aslo try to modify the code to get "[' & $i & ']" only once when the $i was changed.

However, even you wrote the optimized code, I don't think it will run faster than _StringBetween. So don't try to compare with that. 


新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

If I wanted to load in the Events object and search through all the values of a key for a specific one, what method would you recommend? I wasn't sure how to do this optimally that is why i ended up just doing a While..Wend using the $i to increment onto the next key until i find the value i'm after. Also i wasn't sure if   '["events"][' & $i & ']["event_id"]') <> "" was the correct method to check for end of keys. I realise what i'm after is a Jsmn_Search function that looks for a value and returns how to obtain it via Jsmn_Get.

I did a test of copying key values into an array using Jsmn_Get then _StringBetween and Jsmn_Get was a lot faster on a test of about 1600 keys.

Edited by Fly By Night

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Here is the JSMN_Get function that Ward wrote in post #12 written to allow both delimiter notation (dot, comma, pipe, whatever notation) and JSON notation.

; #FUNCTION# ===================================================================
; Name:             Jsmn_Get
; Description:    Retrieves a value from the scripting.dictionary key specified
;
; Parameter(s):
;                       $Object     Scripting dictionary object
;
;                       $Key        Key for the value you want to retrieve. Follows delimiter based or JSON based format depending on the notation parameter
;
;                       $Notation
;                                       0 - Delimiter based - Key1.Key2
;                                       1 - JSON format based ["key1"]["key2"]
;
;                       $Delim      If notation = 0 Then this is the delimiter between keys
;
; Requirement(s):   JSMN JSON library by Ward.
;
; Return Value(s):
;                           On Success  Returns value from key
;
;                           On Failure      Returns a blank string and an error value.
;                                               @error
;                                                   1 - key does not exist
;                                                   2 - notation syntax error
;                                               @Extended - Only used if notation = 0
;                                                   See StringSplit error codes
;
; Author(s):    Ward (modified slightly by SantaRyan to add delimiter notation)
;===============================================================================
Func Jsmn_Get($Object, $Key, $Notation = 0, $Delim = ".")
    If Not $Key Then Return $Object
    If IsKeyword($Notation) Then $Notation = 0
    Local $Index, $Match, $Ret
    If $Notation = 0 Then
        $Match = StringSplit($Key, $Delim, 1)
        If IsArray($Match) Then
            $Index = $Match[1]
            $Key = ""
            For $i = 2 To $Match[0]
                $Key &= $Match[$i] & $Delim
            Next
            $Key = StringTrimRight($Key, 1)
        Else
            Return SetError(2, 0, "")
        EndIf
    ElseIf $Notation = 1 Then
        $Match = StringRegExp($Key, "(^\[([^\]]+)\])", 3)
        If IsArray($Match) Then
            $Index = Jsmn_Decode($Match[1])
            $Key = StringTrimLeft($Key, StringLen($Match[0]))
        Else
            Return SetError(2, 0, "")
        EndIf
    EndIf
    If IsString($Index) And Jsmn_IsObject($Object) And Jsmn_ObjExists($Object, $Index) Then
        $Ret = Jsmn_Get(Jsmn_ObjGet($Object, $Index), $Key, $Delim)
        Return SetError(@error, 0, $Ret)
    ElseIf IsNumber($Index) And IsArray($Object) And $Index >= 0 And $Index < UBound($Object) Then
        $Ret = Jsmn_Get($Object[$Index], $Key, $Delim)
        Return SetError(@error, 0, $Ret)
    Else
        Return SetError(1, 0, "")
    EndIf
EndFunc   ;==>Jsmn_Get
Edited by santaryan

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Hello,

thanks for this UDF, it has been really helpful so far. However, I got one JSON string that seems impossible to decode with this UDF (I validated it with a JSON validator, so does not seem to be invalid):

I doesn't work in this form:

#Include <JSMN.au3>

$JSON='{"a":{"742597609":{"v":673,"p":4.91,"lb":"test1","av":58,"sl":11.6},"534165346":{"v":777,"p":5.66,"lb":"test2","av":58,"sl":13.3}},"b":16092018}'

Local $objJson = Jsmn_Decode($JSON)

If Jsmn_IsObject($objJson) Then
    Local $auctions = Jsmn_ObjGet($objJson, "a")
    For $i = 0 To UBound($auctions)-1
        Local $objJson2 = $auctions[$i]
        ConsoleWrite(Jsmn_ObjGet($objJson2, "p"))
    Next
EndIf

When I transform the JSON string like this, it produces an output:

#Include <JSMN.au3>

$JSON='{"a":{"742597609":{"v":673,"p":4.91,"lb":"test1","av":58,"sl":11.6},"534165346":{"v":777,"p":5.66,"lb":"test2","av":58,"sl":13.3}},"b":16092018}'

;JSMN compatibility
$JSON=StringRegExpReplace($JSON,'"\d{9}":','')
$JSON=StringReplace($JSON,'{"a":{','{"a":[')
$JSON=StringReplace($JSON,'}},','}],')

;produces
;$JSON='{"a":[{"v":673,"p":4.91,"lb":"test1","av":58,"sl":11.6},{"v":777,"p":5.66,"lb":"test2","av":58,"sl":13.3}],"b":16092018}'

Local $objJson = Jsmn_Decode($JSON)

If Jsmn_IsObject($objJson) Then
    Local $auctions = Jsmn_ObjGet($objJson, "a")
    For $i = 0 To UBound($auctions)-1
        Local $objJson2 = $auctions[$i]
        ConsoleWrite(Jsmn_ObjGet($objJson2, "p"))
    Next
EndIf

What am I doing wrong ? Is it possible to get the auction details (like "p" in this example) without transforming the JSON string ?

Edited by level20peon

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
      MessagePack UDF
      By Ward
      Introduction
      "MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller." (by http://msgpack.org/).
      There are already so many languages support MessagePack format, here comes AutoIt version. The backend coder is Charlie Gunyon's cmp. It supported MessagePack proposal v5.
      This UDF only have two public functions: MsgPack_Pack() and MsgPack_Unpack(). It just like Json_Enocde() and Json_Decode() in my ?do=embed' frameborder='0' data-embedContent>>JSON UDF. The Map type will  be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). So I suggest using Json.au3 to deal with the Map/Object data type. The main difference is MessagePack support Binary format, but Json not. For example (?do=embed' frameborder='0' data-embedContent>>Json.au3 is required to run this example):
      Local $Obj1 Json_Put($Obj1, ".binary", Binary("0x00")) Json_Put($Obj1, ".string", "abc") Json_Put($Obj1, ".int", Random(0, 1000, 1)) Json_Put($Obj1, ".float", Random()) Json_Put($Obj1, ".bool", True) Json_Put($Obj1, ".null", Null) Local $Binary = MsgPack_Pack($Obj1) Local $Obj2 = MsgPack_Unpack($Binary) ConsoleWrite("Test2 $Obj1.binary is " & VarGetType(Json_Get($Obj2, ".binary")) & @LF) ; "Binary" In my opinion, for human readable data storing or exchanging, of course choose Json. Otherwise, especially to exchange data on the internet, MessagePack may be a better choice. 
      MsgPack.zip
    • redrider81
      Idea: Feature Rich REST UDF
      By redrider81
      There are some impressive UDFs emerging or being re-written recently surrounding REST services. At the same time, the explosion of useful web services and use-cases to tie them together shows no signs of slowing. I suggest a project to tie together the existing UDFs into a more comprehensive UDF which abstracts the four major functions of working with REST services: 
      Authentication (OAuth1/2, OpenID/Connect, Etc) The HTTP Calls (Flexibility with URL, HTTP Methods, Header, Body) Manipulating the Data (XML and JSON) Local Storage/Caching (A new file-based NoSQL Strategy combined with SQLite) Some of these are pretty tall orders, however the overall notion seems so universally useful with the present technologies, I wanted to suggest it for community feedback to measure support. 
      Full disclosure, I don't have time or skill level to contribute to these UDF's in a meaningful way. I thought maybe I shouldn't even post this for that reason, but I went ahead anyway. 
      The UDFs that I find exciting and think would be good candidates for inclusion are listed below: 
       

       
       
    • ozmike
      OO_JSON.UDF, JSON/path, OO, using javascript in Auto IT
      By ozmike
      Hi
       
      This UDF is for anyone who
      - Object Orientated Programing ie properties and methods in autoIT
      - needs standards compliant JSON,, 
      - JSON.parse (read) and JSON.stringify (to text )
      - Now JSONPath
      - wants to use dot syntax in AutoIT like you would in Javascript 
      - dosen't like AutoIt arrays - full javascript arrays and objects in AutoIT! 
      - knows javascript or would like to learn autoIT or vice versa.
      - call javascript functions like EncodeURI
      - run any javascript from autoIT !
       Tested  IE8 - IE11. v7 JSON_OO v7.zip - IE free version. ( hey microsoft ditched IE now too!) .- No IE dependencies- Added arrayAdd and arrayDel- JSONPath! for searching.- Added Keys Function - to list properties of a JSON object.- Secure JSON parse- Native Pure JS implementaion not a port.Credits to Douglas Crockford's for JSON2 code and stefan.goessner for JSONPath code.
      (see example file).  
       
      v4  - json_oo_v4.zip.  -  use v7 (v4 may not work because of  windows updates on dec-2014. ).
      v4 uses IEs JSON ,  so no ported external librariesv4 also includes a Non - IE version of JSON no include files but no stringify and uses javascript EVal (not secure). use v7.eg valid JSON  '{ "hello" : "world" }' invalid "{ 'hello' : 'world' }"v4 does not have JSONPath , arrayAdd, arrayDel or keys (properties) methods - use v7 
       
       
      - enjoy
      ...the brakes come off Autoit . This is the smallest OO extensions for AutoIT
      Object Oriented JSON 
      --- using JSON to Build Objects in Native AutoIT -- Using JSON to create objects $oJSON = _OO_JSON_Init ( ) $jsObj = $oJSON.parse('{ "hello": "world" }') $jsObj.hello;-> world $oJSON.stringify( $jsObj );-> {"hello":"world"} $jsObj.stringify();-> {"hello":"world"}  
      ARRAYS - goodbye Auto it arrays hooray
      The real magic in this script is that it lets you access JSON array items by index number through dot syntax
      without the need for a for-each loop. eg you can go $obj.arr.item(n) 
      It does this by making javascript objects  syntax friendly to Autoit.

      Also you can go $obj.arr.item("property").and   $obj.arr.property & Also $obj.arr.length (see below)  
      -- Using JSON to create one dimentional array $sJSON=[ "h1", "h2", "h3" ] $oResult.length= 3 $oResult.item(0);-> h1 -- Using JSON to create 2 dimentional array; $sJSON=[[1,2],[3,4],[5,6]] $oResult.length= 3 $oResult.item(0);-> $oResult.item(0).stringify();-> [1,2] $oResult.item(2).item(1);-> 6 $oResult.item(2).length;-> 2 -- Using JSON to create an object array $sJSON= [ { "card":"ace" }, {"card":"king" }] $oResult.length= 2 $oResult.item(0).card;-> ace -- Using JSON to create key values $sJSON= { "name":"Jon" , "surname":"who" } $oResult.item("surname");-> who $oResult.surname ;-> who  Many other benefits such as building objects using JSON text just like you would in JavaScript 
        
      Basic OO - Properties and methods
      -- Add properties to objects in AutoIT $jsObj.propAdd("myProp", "'x'") $jsObj.myProp ;-> x $jsObj.propAdd("myProp", '{ "hello": "world" }') $jsObj.myProp.hello ;-> world -- User Defined methods - using javascript -CASE SENSITIVE $jsObj.methAdd("myMethod", " Function('y','return y*5') " ) $jsObj.myMethod(5) ;-> 25  
      Some people have problems on 64 bit autoIT windows with the script control...here is the work around..
      '?do=embed' frameborder='0' data-embedContent>>
      You will most likely have the script control ..but here it is..
       http://www.microsoft.com/en-us/download/details.aspx?id=1949
       --- using JSON to Build Objects in Native AutoIT -- Using JSON to create objects $oJSON = _OO_JSON_Init ( ) $jsObj = $oJSON.parse('{ "hello": "world" }') -- Accessing Items $jsObj.hello;-> world $jsObj.item("hello");-> world -- Using Any Object to create objects $jsObj.stringify();-> {"hello":"world"} $jsObj = $jsObj.parse('{ "goodbye": "world" }') $jsObj.goodbye;-> world -- Read JSON from a file (PC only) - untested $var = _OO_JSON_Read_File("jsondata.txt") $obj = $oJSON.parse($var) > BASIC OO (Object Oriented) programming in Auto it -- Compound Syntax $oJSON.parse( '{ "hello":"world" }' ).hello;-> world -- assigning propeprties $jsObj.parse ( '{ "goodbye": "world" }') $jsObj.goodbye ;-> world $jsObj.goodbye = "hello" $jsObj.goodbye;-> hello > OO Adding Methods and Properties in Auto it -- Add properties to objects in AutoIT $jsObj.propAdd("myProp", "'x'") $jsObj.myProp ;-> x $jsObj.propAdd("myProp", '{ "hello": "world" }') $jsObj.myProp.hello ;-> world -- User Defined methods - using javascript -CASE SENSITIVE $jsObj.methAdd("myMethod", " Function('y','return y*5') " ) $jsObj.myMethod(5) ;-> 25 > Querying Objects $jsObj = $oJSON.parse('{ "hello": "world" , "Goodbye" : "World" } ') $jsObj.type($jsObj) ->object $jsObj.isArray() ->False -- List object properties or "keys" $jsObj.keys($jsObj).stringify() ->["hello","Goodbye"] -- Querying Objects $jsObj = $jsObj.parse({ "hello" : "world" , "myarray" : [ "item0", 2 , { "jon" : "who"} ] }) > JSON path - always returns an array of matches $jsObj.jsonPath( "$.*").stringify() -> ["world",["item0",2,{"jon":"who"}]] $jsObj.jsonPath( "$..hello").stringify() -> ["world"] $jsObj.jsonPath( "$..myarray").stringify() ->[["item0",2,{"jon":"who"}]] $jsObj.jsonPath( "$..myarray[?(@.jon)]").stringify() ->[{"jon":"who"}] $jsObj.jsonPath( "$..myarray[?(@.jon)]").item(0).stringify() ->{"jon":"who"} > Basic Arrays using JSON -- Querying Arrays $jsObj.myarray.stringify() ->["item0",2,{"jon":"who"}] $jsObj.type($jsObj.myarray) ->object $jsObj.myarray.isArray() ->True $jsObj.myarray.length ->3 $jsObj.type($jsObj.myarray.item(0)) ->string $jsObj.type($jsObj.myarray.item(1)) ->number $jsObj.type($jsObj.myarray.item(2)) ->object > Modifying Arrays using OO -- Empty array; $jsObj = $oJSON.parse('[]') $jsObj.stringify() ->[] $jsObj.isArray( ) ->True -- Add items; $jsObj.arrayAdd( 0, "test0") $jsObj.arrayAdd( 1, "test1") $jsObj2 = $oJSON.parse( '{ "hello" : "world" }') $jsObj.arrayAdd( 2, $jsObj2) $jsObj.stringify() ->["test0","test1",{"hello":"world"}] -- Delete items; $jsObj.arrayDel( 0) $jsObj.stringify() ->["test1",{"hello":"world"}] -- Using JSON to create one dimentional array $sJSON=[ "h1", "h2", "h3" ] $oResult.length= 3 $oResult.item(0);-> h1 -- Using JSON to create 2 dimentional array; $sJSON=[[1,2],[3,4],[5,6]] $oResult.length= 3 $oResult.item(0);-> $oResult.item(0).stringify();-> [1,2] $oResult.item(2).item(1);-> 6 $oResult.item(2).length;-> 2 -- Using JSON to create an object array $sJSON= [ { "card":"ace" }, {"card":"king" }] $oResult.length= 2 $oResult.item(0).card;-> ace -- Using JSON to create key values $sJSON= { "name":"Jon" , "surname":"who" } $oResult.item("surname");-> who $oResult.surname ;-> who > Working with OO Objects -- assigning JSON objects in AutoIT $jsObj = $oJSON.parse( '{ "hello" : "world" }') $jsObj2 = $oJSON.parse( '{}' ) $jsObj2 = $jsObj $jsObj.hello;-> world $jsObj2.hello;-> world -- Assign an JSON object to a property $jsObj = $oJSON.parse( '{ "hello" : "world" }') $jsObj.hello ;-> world $jsObj2.propAdd("myProp") $jsObj2.myProp = $jsObj $jsObj2.myProp ;-> $jsObj2.myProp.stringify() ;-> {"hello":"world"} $jsObj2.myProp.hello ;-> world -- Using Existing JS Objects , Object must exist in the scripting object (not IE) $oJSON2 =$jsObj.objGet('JSON') ; objGet is javascript eval $oResult = $oJSON2.parse('{ "hello":"world" }') $oResult.hello;-> world > Using Javascript functions extending UDF -- Calling javascript standard functions $jsObj.jsFunAdd( "encodeURI") $jsObj.encodeURI( 'te st' );-> te%20st $jsObj.protoAdd("encodeURI", " function (s) { return encodeURI(s); } " $jsObj.encodeURI( 'te st' );-> te%20st -- Calling javascript literal methods $str_obj = $jsObj.toObj("my string") $str_obj.jsMethAdd( "charAt") $str_obj.charAt( 0 );-> m $jsObj.toObj('\"my string').charAt(0) ;-> \ $str_obj.jsFunAdd( "charAt") $str_obj.charAt( 0 );-> m $jsObj.jsMethAdd("toFixed" ) $jsObj.toObj(5.56789).toFixed(2) ;-> 5.57 $jsObj.jsMethAdd("concat" , 3 ) $jsObj.toObj('hello').concat( ' world', ' again ', ' and again ' ) ;-> hello world again and again $jsObj.dot("\""'my string", "charAt(0)" );-> \ > depreciated syntax -- depreciated syntax - previous UDFs $jsObj = _JS_obj_create ( '{ "hello": "world" }') ; invalid "{ 'hello':'world'}" $jsObj.hello ;-> world $jsObj.objToString();-> {"hello":"world"} $jsObj.strToObject ( '{ "goodbye": "world" }') $jsObj.goodbye ;-> world -- Close IE explorer.exe instance - not required anymore _OO_JSON_Quit ( ) -- Objects still usable after _OO_JSON_Quit closes $jsObj.goodbye ;-> world  Example script output; see example file
      ;  
      enjoy
    • guinness
      JSON object regular expression parsing.
      By guinness
      Come up with a different regular expression, maybe improved?
      #include <Array.au3> #include <Inet.au3> Local $aSRE = StringRegExp(_INetGetSource('http://www.telize.com/geoip'), '"\w+":"?([^",]+)', 3) _ArrayDisplay($aSRE)
    • meisandy
      Dealing with JSON response to Twitter API
      By meisandy
      Hi All,
      Long time no post - the forum has changed a lot since I was last here! Hope you are all ok.
      Anyway, the reason I'm here is because I'm writing a program that needs to post a Tweet to a Twitter account. I am using the >Twitter UDF, which I have already adapted slightly so that once a user has logged in once the username and password is not need because the OAuth token and OAuth token secret is stored and used again, rather than regenerating a them each time the program is launched. However, since the UDF was last updated Twitter has updated their API and retired the use of XML so now the response is only given in JSON.
      Fortunately, to get the UDF tweeting again it was simply a case of changing the URL that the data was POSTed to. However, because IE doesn't actually render/display JSON files, whenever the data is sent a File Download dialog appears asking to download the JSON file that is returned.
      Does anyone know a way around this to stop the dialog appearing, other than sending a keypress when the window is detected?
      Thanks in advance