Popular Post Gabriel13 Posted October 18, 2009 Popular Post Posted October 18, 2009 (edited) I started this JSON library back in late 2007, because I needed a way to pass structured data between Javascript HTAs and AutoIt3 scripts. I've recently worked to get it in good enough shape to share with the AutoIt community, and while it still needs a bit more polish, the core functionality should be solid.Three files are included in the attached archive:JSON.au3 – the UDF Library itself. It includes a (very large) comment section at the top, which should provide sufficient documentation on how the various data types are encoded and decoded, as well as the various Javascript-based decoding extensions allowed (single-quoted strings, comments, etc.).JSON_Translate.au3 provides example functions for the powerful "translator" functionality. Translator functions can be used for both encoding and decoding. When encoding, it allows you to recursively convert your data from its normal format into the required data structures used by the JSON UDF library. When decoding, it allows you to convert the data in the opposite direction – from the JSON UDF library's data structures to the data structures your program normally uses. To use this functionality, you'll need to write your own translator functions, but this allows you to meet the custom needs of your own programs without having to hack JSON.au3. The examples provided in this file can probably be improved, but show one way of translating AutoIt-specific data types (such as binary and hwnd) back and forth.testJSON.au3 provides some quick examples of regular and translator-enhanced usage, which also help verify that encoding and decoding are working properly. These tests may not be comprehensive.The area needing the most work is error-handling. JSON data structures can be deeply nested, and it's tricky to make sure errors "bubble up" properly in AutoIt. (Plus, I need a flowchart to keep track of all my setError() numbers!)Thanks to Douglas Crockford of JSON.org, whose 2005 Javascript code for JSON encoding and decoding provided an excellent starting point for this library.Edit: updated to 0.9.1 on 2009-10-19substantially updated opening commentsencoding: implemented logic to return "warning" @error flags for unsupported variable types & JSON objects with non-unique key stringsother minor fixes & tweaksJSON.7z Edited October 20, 2009 by Gabriel13 Wiliat87, MarcElser, nobody0 and 4 others 6 1 [url='http://www.autoitscript.com/forum/index.php?showtopic=104150']JSON UDF Library (fully RFC4627-compliant)[/url][url='http://www.autoitscript.com/forum/index.php?showtopic=104325']Keep Windows Clear of Top Taskbar (for those who like their taskbar on top)[/url]
tomjoad Posted October 19, 2009 Posted October 19, 2009 Thank You for sharing. Just what I was looking for. ;-) /t
Gabriel13 Posted October 20, 2009 Author Posted October 20, 2009 (edited) You're welcome! I'd appreciate any feedback you have on it.In particular, the use of 2D arrays to represent JSON objects, how well does that work for everyone? I believe they should be compatible with the 2D array handling routines from the standard Array.au3 UDF library.I'll be working on adding comments for each function next – hopefully, that'll help me get my error codes organized.Slightly edited for clarity. Edited October 21, 2009 by Gabriel13 LukeLe 1 [url='http://www.autoitscript.com/forum/index.php?showtopic=104150']JSON UDF Library (fully RFC4627-compliant)[/url][url='http://www.autoitscript.com/forum/index.php?showtopic=104325']Keep Windows Clear of Top Taskbar (for those who like their taskbar on top)[/url]
P388l3s Posted December 22, 2009 Posted December 22, 2009 You're welcome! I'd appreciate any feedback you have on it.In particular, the use of 2D arrays to represent JSON objects, how well does that work for everyone? I believe they should be compatible with the 2D array handling routines from the standard Array.au3 UDF library.I'll be working on adding comments for each function next – hopefully, that'll help me get my error codes organized.Slightly edited for clarity.Thanks, a project I'm working spits out JSON formatted files, hopefully I'll be able to use your UDf to make working with these files easier. I like easier!I tip my hat to you.
James Posted January 13, 2010 Posted January 13, 2010 Hmm, this would be useful to use to attempt to implement oAuth! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ
schnibble Posted May 21, 2011 Posted May 21, 2011 is it possible to make this http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/serialization/json/?r=83 with outoit? specially the JSONDecoder.as thanks and best regards
1RV34 Posted February 17, 2012 Posted February 17, 2012 (edited) It seems like this is the only good JSON AutoIt UDF I can find, but how do I cycle through the values? Edited February 17, 2012 by 1RV34 MsgBox(0x40040, "", "Hello Forum!")
guinness Posted March 2, 2012 Posted March 2, 2012 (edited) This version fixes problems with undeclared variables and variables not being used amongst other issues encountered e.g. encoding of the au3 files.Note: I haven't tested, just fixed the errors.JSON.zip Edited June 9, 2012 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
nobody0 Posted November 11, 2012 Posted November 11, 2012 (edited) Thanks for this great work i have been looking for a proper JSON library for 2 days before i found this one! To show my thanks heres a little helper function addition, its purpose is to enable to query the json object in an easier way like "key1.key2", i hope this helps someone =) expandcollapse popup#include <Array.au3> #include "JSON.au3" #include "JSON_Translate.au3" ; examples of translator functions, includes JSON_pack and JSON_unpack Func _JSONGet($json, $path, $seperator = ".") Local $seperatorPos,$current,$next,$l $seperatorPos = StringInStr($path, $seperator) If $seperatorPos > 0 Then $current = StringLeft($path, $seperatorPos - 1) $next = StringTrimLeft($path, $seperatorPos + StringLen($seperator) - 1) Else $current = $path $next = "" EndIf If _JSONIsObject($json) Then $l = UBound($json, 1) For $i = 0 To $l - 1 If $json[$i][0] == $current Then If $next == "" Then return $json[$i][1] Else return _JSONGet($json[$i][1], $next, $seperator) EndIf EndIf Next ElseIf IsArray($json) And UBound($json, 0) == 1 And UBound($json, 1) > $current Then If $next == "" Then return $json[$current] Else return _JSONGet($json[$current], $next, $seperator) EndIf EndIf return $_JSONNull EndFunc ;create an json object to test Local $json = _JSONDecode('{"test":{"x":[11,22,{"y":55}]}}') ;query this object Local $result = _JSONGet($json, "test.x.2") ;query the previous result $result = _JSONGet($result, "y") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF) _ArrayDisplay($result) ;query this object using another seperator $result = _JSONGet($json, "test->x->2->y", "->") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF) ;query this object $result = _JSONGet($json, "test.x.2") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF & @CRLF) _ArrayDisplay($result) ;query this object $result = _JSONGet($json, "test.non.existent.key") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF) Edited November 11, 2012 by nobody0 HudsonKane 1
yahaosoft Posted November 16, 2012 Posted November 16, 2012 great job! i have used in my project,thank you for sharing it! Thanksgiving...
Danp2 Posted November 16, 2012 Posted November 16, 2012 I agree, and I recently used this in a project as well. nobody0's helper function was also a lifesaver and should be added to the main .au3 file. One question: I noticed that @error was 2 after a call to _JSONDecode(). This seems to be coming from the final line of __JSONSkipWhitespace(). Can someone explain the reasoning behind @error being set in this routine? Thx! Latest Webdriver UDF Release Webdriver Wiki FAQs
MrTWist Posted November 21, 2012 Posted November 21, 2012 (edited) Thanks for the effort, Gabriel! I have found that the JSON UDF appears to choke on the following JSON returned from Google Books API, whereas other JSON parsers don't have issues. Specifically, the JSON array for the "items" key is not parsed. expandcollapse popup{ "kind": "books#volumes", "totalItems": 1, "items": [ { "kind": "books#volume", "id": "1sCDyGWDgQsC", "etag": "ND3n2hIHVlg", "selfLink": "https://www.googleapis.com/books/v1/volumes/1sCDyGWDgQsC", "volumeInfo": { "title": "Excel 2007 Advanced Report Development", "authors": [ "Timothy Zapawa" ], "publisher": "Wiley", "publishedDate": "2007-03-26", "description": "Create powerful, innovative reporting solutions with Excel 2007! With this new book, which is significantly updated from the bestselling Excel 2003 version, author Timothy Zapawa provides you with in-depth coverage of Excel 2007’s enhanced reporting capabilities. Discover what you can do with PivotTable and Spreadsheet reports, enhanced conditional formatting and filtering functions, improved data access and management tools, and expanded OLAP features. With notes, tips, warnings, real-world examples, and how-to videos in every chapter, you'll soon be an expert on Excel report development!", "industryIdentifiers": [ { "type": "ISBN_10", "identifier": "0470046449" }, { "type": "ISBN_13", "identifier": "9780470046449" } ], "pageCount": 576, "printType": "BOOK", "categories": [ "Computers" ], "averageRating": 4.0, "ratingsCount": 1, "contentVersion": "preview-1.0.0", "imageLinks": { "smallThumbnail": "http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=5&source=gbs_api", "thumbnail": "http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=1&source=gbs_api" }, "language": "en", "previewLink": "http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&cd=1&source=gbs_api", "infoLink": "http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&source=gbs_api", "canonicalVolumeLink": "http://books.google.com/books/about/Excel_2007_Advanced_Report_Development.html?hl=&id=1sCDyGWDgQsC" }, "saleInfo": { "country": "US", "saleability": "NOT_FOR_SALE", "isEbook": false }, "accessInfo": { "country": "US", "viewability": "NO_PAGES", "embeddable": false, "publicDomain": false, "textToSpeechPermission": "ALLOWED_FOR_ACCESSIBILITY", "epub": { "isAvailable": false }, "pdf": { "isAvailable": false }, "webReaderLink": "http://books.google.com/books/reader?id=1sCDyGWDgQsC&hl=&printsec=frontcover&output=reader&source=gbs_api", "accessViewStatus": "NONE" }, "searchInfo": { "textSnippet": "In this book, Timothy Zapawa has provided you with in-depth coverage of Excel 2007's enhanced reporting capabilities." } } ] } Edited November 21, 2012 by MrTWist
robertcollier4 Posted March 28, 2013 Posted March 28, 2013 (edited) To show my thanks heres a little helper function addition, its purpose is to enable to query the json object in an easier way like "key1.key2", i hope this helps someone =) Thanks nobody0 - this helped me a lot. Here is a complementary _JSONSet() function I wrote to also set JSON entries in the dotsyntax. expandcollapse popup#include #include "JSON.au3" #include "JSON_Translate.au3" ; examples of translator functions, includes JSON_pack and JSON_unpack ;Expands upon _JSONGet from nobody0 ;http://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/#entry1030327 Func _JSONSet($writeValue, ByRef $json, $path, $seperator = ".") Local $seperatorPos, $current, $next, $l $seperatorPos = StringInStr($path, $seperator) If $seperatorPos > 0 Then $current = StringLeft($path, $seperatorPos - 1) $next = StringTrimLeft($path, $seperatorPos + StringLen($seperator) - 1) Else $current = $path $next = "" EndIf If _JSONIsObject($json) Then $l = UBound($json, 1) Local $matchFound = False For $i = 0 To $l - 1 If $json[$i][0] == $current Then $matchFound = True If $next == "" Then $json[$i][1] = $writeValue Return Else _JSONSet($writeValue, $json[$i][1], $next, $seperator) Return EndIf EndIf Next If Not $matchFound Then ReDim $json[UBound($json)+1][2] If $next == "" Then $json[UBound($json)-1][0] = $current $json[UBound($json)-1][1] = $writeValue Else $json[UBound($json)-1][0] = $current Local $newjsonobject[1][2] $newjsonobject[0][0] ="" $newjsonobject[0][1] ='JSONObject' $json[UBound($json)-1][1] = $newjsonobject _JSONSet($writeValue, $json[UBound($json)-1][1], $next, $seperator) Return EndIf EndIf ElseIf IsArray($json) And UBound($json, 0) == 1 Then If UBound($json, 1) > $current Then If $next == "" Then $json[$current] Return Else _JSONSet($writeValue, $json[$current], $next, $seperator) Return EndIf Else ReDim $json[$current+1] Local $newjsonobject[1][2] $newjsonobject[0][0] ="" $newjsonobject[0][1] ='JSONObject' $json[$current] = $newjsonobject If $next == "" Then $json[$current] Return Else _JSONSet($writeValue, $json[$current], $next, $seperator) Return EndIf EndIf Else return $_JSONNull EndIf EndFunc Edited March 28, 2013 by robertcollier4
vortexed Posted February 18, 2014 Posted February 18, 2014 Thanks for sharing this! I will be using this to build a simple jquery grid to display on a website. This UDF will constantly run in a while loop with sleep timers comparing flat file modified date with current systems date and query a SQL server to build the Jason files if the date difference between file date and system date is 1
RickB75 Posted August 24, 2015 Posted August 24, 2015 Quick question guys. With the functions given by nobody0 and robertcollier4 . If I want to loop through a JSON file, will I need to build a loop outside of the function examples provided? In my file I have roughly 1000 sections of data. Within each section is roughly 50 items.
Urbiu Posted November 9, 2016 Posted November 9, 2016 Hello, I am just trying to get in touch with this functions, but I don't know really where to start. I would like to read a JSON package coming from a servery with a string value and save this value to an array. Can I do this with this functions? If yes, please can you show me how should I start. Thank you.
VAN0 Posted March 11, 2017 Posted March 11, 2017 (edited) Sorry, wrong thread... Edited March 11, 2017 by VAN0 Wrong thread
robinj Posted August 10, 2017 Posted August 10, 2017 (edited) I have some JSON in a variable that is totally unformatted as returned from a REST API call. What is the best way to extract the values from it using this UDF. It should be simple, but struggling to get it working as it should Example JSON [{"id":"4434156","url":"https://legacy.sky.com/v2/schedules/4434156","title":"468_CORE_1_R.4 Schedule","time_zone":"London","start_at":"2017/08/10 19:00:00 +0100","end_at":null,"notify_user":false,"delete_at_end":false,"executions":[],"recurring_days":[],"actions":[{"type":"run","offset":0}],"next_action_name":"run","next_action_time":"2017/08/10 14:00:00 -0400","user":{"id":"9604","url":"https://legacy.sky.com/v2/users/9604","login_name":"robin@ltree.com","first_name":"Robin","last_name":"John","email":"robin@ltree.com","role":"admin","deleted":false},"region":"EMEA","can_edit":true,"vm_ids":null,"configuration_id":"19019196","configuration_url":"https://legacy.sky.com/v2/configurations/19019196","configuration_name":"468_CORE_1_R.4"},{"id":"4444568","url":"https://legacy.sky.com/v2/schedules/4444568","title":"468_CORE_1_R.4 Schedule","time_zone":"London","start_at":"2017/08/11 12:00:00 +0100","end_at":null,"notify_user":false,"delete_at_end":false,"executions":[],"recurring_days":[],"actions":[{"type":"suspend","offset":0}],"next_action_name":"suspend","next_action_time":"2017/08/11 07:00:00 -0400","user":{"id":"9604","url":"https://legacy.sky.com/v2/users/9604","login_name":"robin@ltree.com","first_name":"Robin","last_name":"John","email":"robin@ltree.com","role":"admin","deleted":false},"region":"EMEA","can_edit":true,"vm_ids":null,"configuration_id":"19019196","configuration_url":"https://legacy.sky.com/v2/configurations/19019196","configuration_name":"468_CORE_1_R.4"}] Thanks for any help Edited August 10, 2017 by robinj extracting email
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now