Terenz

VBS JSON Parser

11 posts in this topic

#1 ·  Posted (edited)

Hello,

I know there are some UDF for JSON but searching on the web i have found one very small in term of line code:

strJson = CreateObject("scripting.filesystemobject").openTextFile("json.txt").readall

Set j = ParseJson(strJson)
wscript.echo j.location.name
wscript.echo j.current.last_updated

'http://demon.tw/my-work/vbs-json.html
Function ParseJson(strJson)
    Set html = CreateObject("htmlfile")
    Set window = html.parentWindow
    window.execScript "var json = " & strJson, "JScript"
    Set ParseJson = window.json
End Function

And i'd like to convert to Autoit. Plus, i hate JSON since i don't understand it.

 I need to know:

1) Check if the value exist before read it

2) How to access to a value like:

"car": {
        "carday": [
            {
                "date": "2016-11-23",
                "date_epoch": 1479859200,
                "day": {
                    "money": 21.3,

I have try with j.car.carday.date but fail

My attempt to convert that code:

Local $strJson = ObjCreate("scripting.filesystemobject")
$strJson.openTextFile("json.txt").readall

Local $j = ParseJson($strJson)
ConsoleWrite($j.location.name)
ConsoleWrite($j.current.last_updated)

Func ParseJson($strJson)
    Local $html = ObjCreate("htmlfile")
    Local $window = $html.parentWindow
    $window.execScript("var json = " & $strJson, "JScript")
    Return $window.json
EndFunc   ;==>ParseJson

Anyway has some draw back to use that? Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites



First try to add COM ERROR HANDLER


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The error by autoit is:

The requested action with this object has failed for the line with execScript. The COM error is -2147352567

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
Set scriptControl = CreateObject("MSScriptControl.ScriptControl") 
scriptControl.Language = "JScript"    
Set searchResultObject = scriptControl.Eval("(" + json + ")")

With above you can include whole jscript and control it from AutoIT

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If i have understood isn't better to use the 'autoit version' instead of include the VBS inside the script and launch that? I think in some way is faster, dunno.

EDIT: fail also with eval

$window.eval("var json = " & $strJson)
$window.eval("javascript:var json = " & $strJson)

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

in scripting languages speed is normally not an issue. If you need speed write it in assembler.
jscript is probably quicker then AutoIT which probably is quicker then VBScript.

Write it in 3 ways and you will know which one is faster if you process large JSON files

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Assembler lol barely know autoit. At this time the only one that work is the original VBS so i can't make any comparation and still don't understand why on autoit fail. The JSON can be max 2500 lines, i don't know if is "big" is the first time i'll see one.

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
33 minutes ago, Terenz said:

The error by autoit is:

The requested action with this object has failed for the line with execScript. The COM error is -2147352567

Read HelpFile for:
 

ObjEvent()

Focus on first example especialy how this following function is used:
 

Func _ErrFunc($oError)

 


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Test1.au3 (12) : ==> COM Error intercepted !    
    err.number is:      0x80020009
    err.windescription: 
    err.description is: 80020101.
    err.source is:      
    err.helpfile is:    C:\WINDOWS\system32\mshtml.hlp
    err.helpcontext is:     0
    err.lastdllerror is:    0
    err.scriptline is:  12
    err.retcode is:     0x80020101

Test1.au3 (13) : ==> COM Error intercepted !
    err.number is:      0x80020006
    err.windescription: 
    err.description is: 80020101.
    err.source is:      
    err.helpfile is:    C:\WINDOWS\system32\mshtml.hlp
    err.helpcontext is:     0
    err.lastdllerror is:    0
    err.scriptline is:  13
    err.retcode is:     0x80020101

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

As far as I know 

$strJson.openTextFile("json.txt").readall

ReadAll is parameter not method . Check this on MSDN.

1 person likes this

NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

What? Yes that was the problem! Now works:

Local $hFileOpen = FileOpen(@ScriptDir & "\json.txt")
If $hFileOpen = -1 Then Exit
Local $sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)

Local $JSON = ParseJson($sFileRead)
ConsoleWrite($JSON.location.name & @CRLF)
ConsoleWrite($JSON.current.last_updated & @CRLF)

Func ParseJson($strJson)
    Local $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $html = ObjCreate("htmlfile")
    Local $window = $html.parentWindow
    $window.execScript("var json = " & $strJson, "JScript")
    Return $window.json
    #forceref $oError
EndFunc   ;==>ParseJson

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

I'd like to replace execScript with eval and i still need to understand how to access subtext or whatever is called.


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

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