Jump to content

"JSON UDF in pure AutoIt" and array count


Go to solution Solved by OJBakker,

Recommended Posts


Hello,

is there any reason why i can't make this example from the german forum output the array size?

#include <JSON.au3>

Example4()

Func Example4()
    ConsoleWrite(@CRLF & @CRLF)
    ConsoleWrite(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" & @CRLF)

    Local $sJSONTest = '[{"_object": "typ","card": "test1"},{"_object": "typ","card": "test2"},{"_object": "typ","card": "test3"},{"_object": "typ","card": "test4"}]'

    Local $o_Object = _JSON_Parse($sJSONTest)
    ConsoleWrite("$o_Object: " & _JSON_Generate($o_Object) & @CRLF)

    $vTmp = _JSON_Get($o_Object, "[1]")
    ConsoleWrite("$vTmp: " & _JSON_Generate($vTmp) & @CRLF)
    ConsoleWrite("$vTmp.Count: " & $vTmp.Count & @CRLF)

    ;Hier würde ich gerne alle enthaltenden ITEMS zählen, in dem Beispiel wären es 4 und in einer Schleife einzeln durchgehen

EndFunc

I can see no value for $vTmp.Count.

Regards

Sascha

Link to comment
Share on other sites

#include <JSON.au3>  ; https://www.autoitscript.com/forum/topic/209502-json-udf-in-pure-autoit/?tab=comments#comment-1512704

Example4()

Func Example4()
    ConsoleWrite(@CRLF & @CRLF)
    ConsoleWrite(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" & @CRLF)

    Local $sJSONTest = '[{"_object": "typ","card": "test1"},{"_object": "typ","card": "test2"},{"_object": "typ","card": "test3"},{"_object": "typ","card": "test4"}]'

    Local $o_Object = _JSON_Parse($sJSONTest)
    ConsoleWrite("$o_Object cnt=" & UBound($o_Object) & @CRLF & @CRLF)

    For $i = 0 To UBound($o_Object, 1) - 1
        $mMap = _JSON_Get($o_Object, "[" & $i & "]")
        ConsoleWrite($i & ") $mMap cnt=" & UBound($mMap) & @CRLF)
        Local $aMapKeys = MapKeys($mMap)
        For $vKey In $aMapKeys
            ConsoleWrite("-- Key: " & $vKey)
            ConsoleWrite(@TAB & "Value: " & $mMap[$vKey])
            ConsoleWrite(@TAB & "Variable Type: " & VarGetType($vKey) & @CRLF)
        Next
        ConsoleWrite("" & @CRLF)
    Next

    ;Hier würde ich gerne alle enthaltenden ITEMS zählen, in dem Beispiel wären es 4 und in einer Schleife einzeln durchgehen

EndFunc   ;==>Example4

if you want more follow the link below and have a look at Reveal hidden contents

 

I know that I know nothing

Link to comment
Share on other sites

3 hours ago, Sascha said:

I can see no value for $vTmp.Count.

In the old version of this udf i used the Scripting.Dictionary to implement a json object.

Since maps are part of the stable releases of AutoIt i switched to the inbuild map datatype.

So in former versions you have to use .Count because a Dictionary is used and in the current versions you have to use Ubound() because objects are implemented as AutoIt-Maps.

 

Link to comment
Share on other sites

  • 1 year later...

I want to analyse Thunderbird json files and I just started using this UDF. However when I finally ran the program I had a weird error. When I run my program uncompiled everything works but as soon as I compile the program and run it, it crashes on this line:

Local $o_Current[]

It says array badly formatted. Does this mean that the JSON file is not correctly formatted? But why does it work uncompiled. I don't understand the code enough to figure out what is happening. Any help gratefully received.

Link to comment
Share on other sites

Posted (edited)

Sorry to be dumb, but I thought I was on the latest version (3.3.16.1). Also that wouldn't explain why it crashed when compiled and not when run precompile. In addition, I've compiled a short script that just does the _JSON_Parse and it doesn't crash. I just can't make sense of it at all. I don't know why that line would crash.

Edited by Graeme
Link to comment
Share on other sites

It's true that it may be running in x86 but compiled in x64. How would that make it crash and if so how could I get around it. It's true that the test script I mentioned was in x86 and the script that crashed was in x64.....

The JSON file was the preferences file for Edge.

The test script:

$ScriptDrive = "U:\"
    If FileExists($ScriptDrive & "Settings\Edge\Default\Preferences") Then
        Dim $Fields[6] = ["local_browser_data_share.enabled","download.default_directory","savefile.default_directory", _
        "third_party_search.consented","user_experience_metrics.personalization_data_consent_enabled","ntp.enable_prerender"]
        ;$Fields are the values that we want to verify
        $Changed = False
        $Data = FileRead($ScriptDrive & "Settings\Edge\Default\Preferences")
        $oObject = _JSON_Parse($Data)
        For $R = 0 To UBound($Fields) - 1
            $Setting = _JSON_Get($oObject, "." & $Fields[$R])
            If $R > 0 and $R < 3 Then
                If MsgBox(1,$Fields[$R],$Setting) = 2 Then Exit
            EndIf
            If StringInStr($Fields[$R],"Default_directory") = 0 Then
                If $Setting <> "False" Then
                    _JSON_addChangeDelete($oObject,$Fields[$R],"False")
                    $Changed = True
                EndIf
            Else
                If StringLeft($Setting,3) <> $ScriptDrive Then
                    _JSON_addChangeDelete($oObject,$Fields[$R],$ScriptDrive & "Downloads")
                    $Changed = True
                EndIf
            EndIf
        Next
        If $Changed Then; Update the Preferences File
            MsgBox(0,"","Changed " & _JSON_Get($oObject,"." & $Fields[1]))
            $success = 1
            $hFile = FileOpen($ScriptDrive & "Settings\Edge\Default\Preferences",2)
            $result = FileWriteLine($hFile,_JSON_GenerateCompact($oObject))
            If $result <> $success Then MsgBox(0,"ERROR","The file write didn't happen!" & @CRLF & FileGetAttrib($ScriptDrive & "Settings\Edge\Default\Preferences"))
            FileClose($hFile)
            MsgBox(0,"",$result)
        EndIf
    EndIf

 

Link to comment
Share on other sites

It does not crash on me.  Compiled in x64, works fine.  So there is something buggy in your side.  Maybe AV (look at log) ?

Put some tracing in your script to see where it crashed (on which exact statement), see if it is always on the same line...

Link to comment
Share on other sites

Hey, that line is not even in the script you provided !  So maybe you should think of making a real test case of your script that is runable to us and will show the error.  Until then I am out of here.

Link to comment
Share on other sites

Posted (edited)

Graeme you are not giving us all your details.
E.G. since you're using JSON.au3, there will be a #include <JSON.au3> command somewhere.
Even though I don't see it, I place it, and despite this I reach the maximum of 550 rows.
In the message you posted, it says that an error was found in the 3659 row (which means that there are more)
So you have to show us the whole script, if you want us to take an edge

as the console content would also help to understand what version of AutoIt you are running, since as mentioned above the "Local $o_Current[]" you provide above
refers to a Map variable, which is available in the latest AutoIt Version 3.3 .16.1

run it next, what does it say?

ConsoleWrite("AutoItVersion:" & @AutoItVersion & @CRLF)

 

1 hour ago, Graeme said:

It looks like there's a problem with the UDF

incidentally in the udf folder autoit-json-udf-master there is the example JSON-Test.au3.
You tried it  ?
does it work for you ?

for me it works fine

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

Posted (edited)
1 hour ago, Graeme said:

It looks like there's a problem with the UDF

I've run into problems with this UDF on a project. And because I don't personally know anything thoroughly, I just chose another UDF ( don't remember which UDF ). 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 2 weeks later...

OK. I got a bit involved with other things.

Here is the parred down script with all the stuff that was commented out before, gone.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_type=a3x
#AutoIt3Wrapper_Outfile=U:\Documents\z Graeme and Helen\QA files\QuickAccess3.0\TestJSON.a3x
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#comments-start
Title   : Test
Author  : Graeme
Desc    : Test JSON Udf
#comments-end
#include <json.au3>
    $ScriptDrive = "U:\"
    If FileExists($ScriptDrive & "Settings\Edge\Default\Preferences") Then
        Dim $Fields[6] = ["local_browser_data_share.enabled","download.default_directory","savefile.default_directory", _
        "third_party_search.consented","user_experience_metrics.personalization_data_consent_enabled","ntp.enable_prerender"]
        ;$Fields are the values that we want to verify
        $Changed = False
        $Data = FileRead($ScriptDrive & "Settings\Edge\Default\Preferences")
        $oObject = _JSON_Parse($Data)
        For $R = 0 To UBound($Fields) - 1
            $Setting = _JSON_Get($oObject, "." & $Fields[$R])
            If $R > 0 and $R < 3 Then
                If MsgBox(1,$Fields[$R],$Setting) = 2 Then Exit
            EndIf
            If StringInStr($Fields[$R],"Default_directory") = 0 Then
                If $Setting <> "False" Then
                    _JSON_addChangeDelete($oObject,$Fields[$R],"False")
                    $Changed = True
                EndIf
            Else
                If StringLeft($Setting,3) <> $ScriptDrive Then
                    _JSON_addChangeDelete($oObject,$Fields[$R],$ScriptDrive & "Downloads")
                    $Changed = True
                EndIf
            EndIf
        Next
        If $Changed Then; Update the Preferences File
            MsgBox(0,"","Changed " & _JSON_Get($oObject,"." & $Fields[1]))
            $success = 1
            $hFile = FileOpen($ScriptDrive & "Settings\Edge\Default\Preferences",2)
            $result = FileWriteLine($hFile,_JSON_GenerateCompact($oObject))
            If $result <> $success Then MsgBox(0,"ERROR","The file write didn't happen!" & @CRLF & FileGetAttrib($ScriptDrive & "Settings\Edge\Default\Preferences"))
            FileClose($hFile)
            MsgBox(0,"",$result)
        EndIf
    EndIf

As I said I compiled this as an a3x file and ran it with this line..

MsgBox(0,"",Run('C:\Program Files\Qa\Autoit3.exe "U:\Documents\z Graeme and Helen\QA Files\QuickAccess3.0\TestJSON.a3x"'))

It ran and I got this error. I inserted msgbox lines before and after the line that had always given issues like this in the UDF

if MsgBox(1,"","Before line" & @CRLF & $s_String) = 2 Then Exit
        Local $o_Current[]
If MsgBox(1,"","After line") = 2 Then Exit

I got the first msgbox with the contents of the preferences file as expected then this.

image.png.2861aefe00c9905b355247cb79cbc2db.png

Here is the console output to show versions of AutoIT3 etc

>"C:\Program Files (x86)\AutoIT3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIT3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "U:\Documents\z Graeme and Helen\QA files\QuickAccess3.0\Test.au3" /UserParams    
+>09:52:58 Starting AutoIt3Wrapper (21.316.1639.1) from:SciTE.exe (4.4.6.0)  Keyboard:00000809  OS:WIN_11/2009  CPU:X64 OS:X64  Environment(Language:0809)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper
>Running AU3Check (3.3.16.1)  from:C:\Program Files (x86)\AutoIt3  input:U:\Documents\z Graeme and Helen\QA files\QuickAccess3.0\Test.au3
+>09:52:58 AU3Check ended.rc:0
>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "U:\Documents\z Graeme and Helen\QA files\QuickAccess3.0\Test.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.

 

Link to comment
Share on other sites

I may have found the issue. I decided to run the file with a different executable, one that I have recently compiled and this worked. So the problem seems to be in AutoIT3.exe handling Maps.....

MsgBox(0,"",Run('U:\Settings\QuickAccess\OpenWebLink64.exe "U:\Documents\z Graeme and Helen\QA Files\QuickAccess3.0\TestJSON.a3x"'))

I don't know if this means that something in AutoIT3.exe needs to be updated but I can't use that because the AutoIT3.exe is already deployed over how ever many of the company are using the program - I'll have to move away from using AutoIt3.exe to run a3x files as I see it.

Edited by Graeme
Link to comment
Share on other sites

15 hours ago, Graeme said:
MsgBox(0,"",Run('C:\Program Files\Qa\Autoit3.exe "U:\Documents\z Graeme and Helen\QA Files\QuickAccess3.0\TestJSON.a3x"'))

C:\Program Files\Qa\Autoit3.exe  the address here does not match the one on the console C:\Program Files (x86)\AutoIt3

Edit:

Start with something simple first to make sure everything works. And step by step you fill in the rest

I changed some settings from $Fields[6] because I couldn't find them (at least to me)

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_type=a3x
#AutoIt3Wrapper_Outfile=U:\Documents\z Graeme and Helen\QA files\QuickAccess3.0\TestJSON.a3x
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <json.au3>

_TestJSON()

Func _TestJSON()
    Local $ScriptDrive = "U:\"
    If FileExists($ScriptDrive & "Settings\Edge\Default\Preferences") Then

        Dim $Fields[6] = [".account_info[0].edge_account_first_name", ".account_info[0].email", ".savefile.default_directory", _
                ".third_party_search.consented", ".user_experience_metrics.personalization_data_consent_enabled", ".web_app.system_installed_label_updated"]

        Local $Data = FileRead($ScriptDrive & "Settings\Edge\Default\Preferences")
        Local $oObject = _JSON_Parse($Data)
        Local $Setting = ""

        For $R = 0 To UBound($Fields) - 1
            $Setting &= $R & ") " & _JSON_Get($oObject, $Fields[$R]) & @CRLF
        Next
        MsgBox(0, "JSON Setting:", $Setting)
    EndIf
EndFunc   ;==>_TestJSON

 Try it first without compiling it.

Edited by ioa747

I know that I know nothing

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...