Jump to content

Recommended Posts

Posted
  On 2/2/2015 at 2:05 AM, ozmike said:

 

Ok this is what you want ...i'll leave it to you to play with it...

 

'?do=embed' frameborder='0' data-embedContent>>

; add to OO_JSON
#AutoIt3Wrapper_Res_File_Add=json2.txt, rt_rcdata, JSON_TXT_2

#include "resources.au3"

; $json_text = _OO_JSON_Read_File("json2.txt") ; replace this line in oo_json.udf - do for each file,
$json_text = _ResourceGetAsString("JSON_TXT_2")

 

 

 

Well, interestingly... This is not what I was looking for... although I would have been wanting this someday.  I would have resorted to FileInstall when it came time to deploy our JSON tool for people to use.  This is MUCH better. 

My goal was to have all required code contained in the AU3 for the developers purposes.  As I think more about it, perhaps this is impossible.  It would require a program that actually converts a resource to a binary string that you could copy and paste into the AU3 , and then a new Autoit function like "AutoIt Parse Binary" that you would put that string into.  This would eliminate the quotes issue for ascii strings at least. 

Actually... with that in mind I just had an idea.  There are some encryption functions i used about 8 years ago for passwords... but any simple hash function would do the trick actually. Take a string of text, hash it, and then in the AU3 file you can just put the hashed string with the unhash function.  That i can probably do. 

Posted

The resource udf  I think - lets you at compile time include the text file in the exe which the code can access instead of disk. So no files on disk only one exe.

File Install puts files into the directory from the exe ( if i understand).

Anyway what ever works best for you..

  • 1 month later...
Posted

Hi Mike,  

I think there might be some issue with the expression-based json path.  All results seem to be matching the regular expression, and in the results that are returned, it's replacing the matched text with "-1".  Please test and confirm. 

I pretty much took it verbatim from here: 

https://github.com/jayway/JsonPath

$oJSON = _OO_JSON_Init()

$sBooks = '{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},"expensive":10}'

$jsObjBooks = $oJSON.parse($sBooks )

consolewrite($jsObjBooks.jsonPath("$..book[?(@.author =~ /.*REES/i)]").stringify())
Posted (edited)

Hi Again - Yes does error

 

The web page  of json path you are refering is for JAVA not javascript and it has been modified to support regular expressions. It is not 

the original version of json path.

 

 see JSON path's creater's web page  the example you refer doesn't exist ! see http://goessner.net/articles/JsonPath/

so I guess..if you can find a js version  that supports regex in javascript

and then replace line in the OO_JSON.udf with the new version.

;$jsonPath_text = _OO_JSON_Read_File("jsonpath-0.8.0.js")

$jsonPath_text = _OO_JSON_Read_File("jsonpath-newversion.js")

 Or enhance Jsonpath open source project! regex would be nice!

https://github.com/s3u/JSONPath

Thanks for picking that up

Edited by ozmike
  • 1 year later...
Posted (edited)

@ozmike

I'm working on TeamViewer API.
Could you show me how to parse this string:
 

  Quote

{"records":[{"id":"6cbc3cbf-ee34-43fb-96b5-167813941a38","userid":"u21228XXX","deviceid":"650077XXX","devicename":"Bytom.XXX","groupid":"g25430XXX","groupname":"Clients","start_date":"2014-03-12T07:33:32Z","fee":0.0,"currency":"Zloty","billing_state":"Bill","notes":"teamviewer"},{"id":"dec5f572-d1d2-479d-9cc3-19092ec67c92","userid":"u21228XXX","deviceid":"454337XXX","devicename":"Dąbrowa.XXX","groupid":"g25430XXX","groupname":"Clients","start_date":"2014-05-12T12:39:03Z","fee":0.0,"currency":"Zloty","billing_state":"Bill","notes":"import","contact_id":"c124337XXX"},{"id":"5cf0a0e3-ca62-49c5-a1ef-a73e6ccd4150","userid":"u21228XXX","deviceid":"931603XXX","devicename":"Dąbrowa.XXX","groupid":"g25430XXX","groupname":"Clients","start_date":"2014-03-27T12:27:54Z","fee":0.0,"currency":"Zloty","billing_state":"Bill","notes":"templates"},{"id":"b85dbb8b-2c27-4d47-80b1-c41ef0a0ca79","userid":"u21228XXX","deviceid":"306385XXX","devicename":"Chorzów.XXX","groupid":"g26907XXX","groupname":"Servers","start_date":"2016-05-18T23:57:13Z","fee":0.0,"currency":"Zloty","billing_state":"Bill","contact_id":"c175015XXX"},],"records_remaining":10913,"next_offset":"1fede496-fb46-40ca-8a73-832bb53b2fba"}

Expand  

 

For example I want to get the selected data .

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 2 weeks later...
Posted

Hi you have a syntax error in your example.

Make sure you use  a json parser eg. http://json.parser.online.fr/ as my udf assumes / doesn't tell you if its valid json.  

It could be modified to tell if valid json ...feel free to modify I think it needs an error handler.!

Here is the code to get the 3rd record 

#include <OO_JSON.au3>
$myjsondata = _OO_JSON_Read_File( "jsondata.txt")
$oJSON = _OO_JSON_Init()
$jsObj = $oJSON.parse($myjsondata) ; JSON encode key value
ConsoleWrite("$jsObj.records.item(2).stringify() ;-> " & _
$jsObj.records.item(2).stringify() & @CR)

 

  • 7 months later...
Posted

Made first steps with this one. Looks promising.

 

But there are two things I could not figure, also not from examples.

Given the following JSON structure:

{
    "Name" : "Documentation",
    "Source" : ".",
    "Destination" : "..\\",
    "Pages" : 
    {
        "abc" : "xxx" ,
        "Configuration" :
        {
            "Here is my first key with a long name" : "test.md",
            "Another key with a strange name" : "this.md"
        },
        "def" : "blah"
    }
}

I can get each part no problem. But as can be seen from "Configuration" the keys are not known, and may have long names.

This leads me to two questions:

1) while iterating over "Pages", how can I know the type (differ "abc"  = string from "Configuration" = object). I did try to use .type but always get "string" back.

2) while iterating over "Configuration", I get a lot of entries for the code below. I expected only two. And
   2a) is there way to access configuration by index directly ?

 

Here is what I tried so far:

$oJSON = _OO_JSON_Init()
$jsObj = $oJSON.parse(FileRead("docnet.json"))

$keys = $jsObj.keys($jsObj.Pages)

for $k in $keys
   if StringLen( $k) > 0 then   ;- I get a lot of empty here so have to put in this test. Not sure why ? 
       ConsoleWrite(">>>" & $k & " >>>   " )
       $t = $jsObj.Pages.type($k)  ;-  This always returns "string".
       ConsoleWrite("Type is ==> " & $t & @CRLF)
       ConsoleWrite($jsObj.Pages.item($k) & @CRLF)
   endif
Next

 

Thanks :)

 

  Reveal hidden contents

I am just a hobby programmer, and nothing great to publish right now.

  • 2 months later...
Posted (edited)

It's not working for me.

#include <OO_JSON.au3>

$oJSON = _OO_JSON_Init()
MsgBox(0, @ScriptName, IsObj($oJSON))

This gives me 0.

I am using AutoIt 3.3.10.2 on Windows 10.

Sorry my mistake. Works perfect!

Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

Hi myicq, sorry to get back so late

The reason that you always get string is the keys are just labels the thing after the : is the object !

ie "Configuration" = string , the bit after colon is an object!

This code below returns object.

It hopefully also answers 2a your other question can you directly access the config -yes

then get the keys as shown. 

$t = $jsObj.Pages.Configuration
ConsoleWrite("$jsObj.Pages.configuration  ->" & $t.type($t) & @CR) ;
$t = $jsObj.Pages.item("Configuration")
ConsoleWrite("$jsObj.Pages.configuration  ->" & $t.type($t) & @CR) ;
$keys = $jsObj.keys($t)

for $k in $keys
ConsoleWrite(">>>" & $k & " >>>   " )
Next
 

btw the keys function does seem to return a lot of noise ..I got it off the net ..will have to look into it..

 

Edited by ozmike
format code
  • 5 months later...
Posted (edited)

One could call this "v8".

Performed:

  • Reduced the number of global scope variables and prefixed with "$g_OO_JSON_".
  • Set all global-scope func prefixes to "_OO_JSON_".
  • Renamed the two of the three read files, updated the JSON2.js file from Douglas Crockford, to incorporate some semblance of dependency version.
  • Added optional argument to Object.prototype.stringify function allowing "The space argument" for JSON (prettifying, beautifying, formatting, indenting) whatever you want to call it.

Simple Usage (using random JSON data from json-generator and minifying into single line < 4094 char limit):

#Region
#AutoIt3Wrapper_UseX64=N
#EndRegion

#include "OO_JSON.au3"

Global $oJSON = _OO_JSON_Init()
Global $sJSON  = '[{"_id":"59d5503d51066f85f70f5449","index":0,"guid":"d81f15cf-a242-49b7-8490-fa6efa3bc1d8","isActive":false,"balance":"$2,147.69","picture":"http://placehold.it/32x32","age":40,"eyeColor":"brown","name":"Morgan Walters","gender":"female","company":"CALCU","email":"morganwalters@calcu.com","phone":"+1 (901) 518-3374","address":"206 Neptune Court, Blackgum, Virgin Islands, 6507","about":"Magna cillum anim aute eu Lorem magna nostrud occaecat ipsum sunt do dolore ex. Nulla in cillum nostrud ex cillum. Eu deserunt eu ipsum reprehenderit magna Lorem nulla deserunt et.\r\n","registered":"2017-03-05T06:08:53 +06:00","latitude":-62.021595,"longitude":-36.598964,"tags":["sunt","nulla","dolor","laboris","reprehenderit","eu","fugiat"],"friends":[{"id":0,"name":"Cherie Downs"},{"id":1,"name":"Bailey Carpenter"},{"id":2,"name":"Soto Dickerson"}],"greeting":"Hello, Morgan Walters! You have 6 unread messages.","favoriteFruit":"strawberry"},{"_id":"59d5503d31cda00baaea1b88","index":1,"guid":"bd93c927-93b5-44b4-91e4-d975703224d8","isActive":false,"balance":"$3,463.57","picture":"http://placehold.it/32x32","age":24,"eyeColor":"green","name":"Amber Harrison","gender":"female","company":"RAMEON","email":"amberharrison@rameon.com","phone":"+1 (950) 491-2353","address":"933 Ebony Court, Dyckesville, Texas, 5175","about":"Ullamco eu mollit ex occaecat. In Lorem esse aute ipsum dolore pariatur. Excepteur quis pariatur quis in cillum quis. Anim minim excepteur aliquip proident veniam. Fugiat elit est occaecat nostrud culpa.\r\n","registered":"2015-01-28T05:56:10 +06:00","latitude":-10.900578,"longitude":157.317571,"tags":["sunt","nostrud","irure","qui","ea","ipsum","non"],"friends":[{"id":0,"name":"Harriet Mayer"},{"id":1,"name":"Bartlett Acosta"},{"id":2,"name":"Benita Parker"}],"greeting":"Hello, Amber Harrison! You have 2 unread messages.","favoriteFruit":"strawberry"},{"_id":"59d5503d4025e6661b392115","index":2,"guid":"4b48d82c-9947-43d2-be74-d65f0dbb4d8d","isActive":true,"balance":"$3,816.41","picture":"http://placehold.it/32x32","age":33,"eyeColor":"blue","name":"Singleton Phelps","gender":"male","company":"PETICULAR","email":"singletonphelps@peticular.com","phone":"+1 (959) 554-2977","address":"784 Herkimer Court, Byrnedale, Palau, 1264","about":"Nisi laboris sit irure irure ullamco veniam minim. Pariatur a dolor irure nostrud. Laborum sint est nulla ad quis amet exercitation non. Tempor cupidatat fugiat do adipisicing in ullamco deserunt pariatur. Excepteur anim consectetur ullamco anim eiusmod non mollit ullamco ullamco.\r\n","registered":"2017-06-04T09:01:10 +05:00","latitude":-63.826346,"longitude":125.977731,"tags":["id","culpa","ipsum","ea","irure","dolore","ullamco"],"friends":[{"id":0,"name":"Jolene Gibbs"},{"id":1,"name":"Emma Marquez"},{"id":2,"name":"Wood Chavez"}],"greeting":"Hello, Singleton Phelps! You have 5 unread messages.","favoriteFruit":"banana"},{"_id":"59d5503dfec0ef306a7f20f3","index":3,"guid":"bf7ce964-98c4-4e1e-900d-d88175385c3b","isActive":false,"balance":"$2,224.01","picture":"http://placehold.it/32x32","age":29,"eyeColor":"blue","name":"Amy Paul","gender":"female","company":"OPTIQUE","email":"amypaul@optique.com","phone":"+1 (908) 405-3311","address":"566 Garfield Place, Sims, Indiana, 3274","about":"Eu labore consectetur amet ut sit amet fugiat elit ea consequat. Consequat mollit ullamco adipisicing est pariatur quis minim. Eu duis nulla. Reprehenderit qui consectetur consectetur ullamco esse nisi voluptate.\r\n","registered":"2014-02-12T09:47:11 +06:00","latitude":-71.034296,"longitude":89.195964,"tags":["consectetur","culpa","ad","ea","sunt","ut","fugiat"],"friends":[{"id":0,"name":"Kathie Rivas"},{"id":1,"name":"Doris Pace"},{"id":2,"name":"Harris Hunt"}],"greeting":"Hello, Amy Paul! You have 10 unread messages.","favoriteFruit":"strawberry"}]'
ConsoleWrite("$jsObj.stringify() --> " & @CR & $oJSON.parse($sJSON).stringify(Null, @TAB) & @CR)

The following is confirmed to work on WIndows 10 Pro and Server 2016.

Be sure you set to use 32-bit only in your program since Microsoft Script Control (%WINDIR%\SysWow64\msscript.ocx) is 32-bit only.

#Region
#AutoIt3Wrapper_UseX64=N
#EndRegion
Global $g_OO_JSON_non_IE
Global $g_OO_JSON_JS_framework = _OO_JSON_SET_Framework()
Global $g_OO_JSON_JS_HTML = _OO_JSON_SET_HTML()




;~ ____________________________________________________________________________
Func _OO_JSON_SET_Framework()
;~  https://github.com/douglascrockford/JSON-js
;~  http://goessner.net/articles/JsonPath/
;~  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
;~  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys$revision/670421

    Local $_OO_JSON_JSON2 = _OO_JSON_Read_File("json2_2016-10-28.js")                                                                           ; Base Douglas Crockford JSON ver 2 (parseJSON replaced with JSON.parse) functionality
    Local $_OO_JSON_JSONPATH = _OO_JSON_Read_File("jsonpath-0.8.0.js")                                                                          ; Adds jsonPath functionality
    Local $_OO_JSON_KEYSPOLLYFILL = _OO_JSON_Read_File("js.keys.polyfill_2014-09-23.js")                                                        ; Adds KEYS functionality

    $JSON_framework = "" & _
            $_OO_JSON_JSON2 & _
            " Object.prototype.stringify                = function( a, b ) { return JSON.stringify(this, a, b) } ; " & _                        ;~ Print out an object
            " Object.prototype.parse                    = function( s ) { return JSON.parse(s) } ; " & _                                        ;~ JSON String to object
            " Object.prototype.jsonPath                 = function( expr, arg ) { return JSON.jsonPath(this, expr, arg) } ; "                   ;~ Query object

    $JSON_path_framework = "" & _
            " var JSON = new Object(); JSON.jsonPath = function( obj,expr, arg ) { return jsonPath(obj,expr, arg) };  " & _
            $_OO_JSON_JSONPATH

    ; build Objects in AUTO IT!
    Local $OO_extra = "" & _
            " Object.prototype.propAdd                  = function(prop, val ) { eval( 'this.' + prop + '=' + val )   } ; " & _
            " Object.prototype.methAdd                  = function(meth, def ) { eval( 'this.' + meth + '= new ' + def )   } ; "

    ; extend javascript within AutoIT! Add a method to all objects
    Local $OO_extend_framework = "" & _
            " Object.prototype.protoAdd                 = function( methName, jsFunction , objectTypeName ) { " & _
            " objectTypeName = objectTypeName || 'Object'; eval( objectTypeName + '.prototype.' +  methName + '=' + jsFunction ) } ; "

    ; methods to  add native JS functions like 'URIencode'
    Local $OO_js_native = "" & _
            "Object.prototype.jsFunAdd                  = function( funname , numParams , objectTypeName ) { " & _
            "   var x = buildParamlist ( numParams )  ;  objectTypeName = objectTypeName || 'Object'; " & _
            "   return eval( objectTypeName + '.prototype.' + funname + ' = function(' + x + ') { return ' + funname + '(' + x + '); }' ) ;" & _
            "} ; " & _
            "function buildParamlist ( numParams )   { " & _
            "   var x = ' p0' ; numParams = numParams || 1 ; " & _
            "   for (var i=1; i< numParams; i++) { x=  x + ' , ' + 'p'  + i ;  } ; " & _
            "   return x; " & _
            "} ;"

    ; poly fill the scripting object to give us JSON, JSONpath and Keys and useful functions for access JS objects in autoit..
    Local $JSON_COM_extentions = "" & _
            $_OO_JSON_KEYSPOLLYFILL & _
            " Object.prototype.objGet                   = function( s ) { return eval( s ) } ; " & _                                            ;~ Allows an obj $obj.objGet('JSON')
            " Array.prototype.item                      = function( i ) { return this[i] } ; " & _                                              ;~ Allows arrays to work in AutoIT obj.item(0)'
            " Object.prototype.item                     = function( i ) { return this[i] } ; " & _                                              ;~ Allows dynamic key values to be obtained eg. obj.item('name') or just obj.surname
            " Object.prototype.keys                     = function( ) { if (typeof this == 'object') { return Object.keys(this); } } ;" & _     ;~ Get JSON obj properties method 1 - not recommended
            " Object.prototype.keys                     = function( o ) { if (typeof o == 'object') { return Object.keys(o); } }    ; " & _     ;~ Get JSON obj properties method 2 - recommended usage
            " Object.prototype.arrayAdd                 = function( i, o ) { this[i] = o; } ; " & _                                             ;~ Add item to array
            " Object.prototype.arrayDel                 = function( i ) { this.splice(i,1); }; " & _                                            ;~ Del item from array
            " Object.prototype.isArray                  = function( ) { return this.constructor == Array; } ; " & _                             ;~ Assert obj IsArray
            " Object.prototype.type                     = function( ) { return typeof(this); } ; " & _                                          ;~ Get JSON obj type method 1 - not recommended
            " Object.prototype.type                     = function( o ) { return typeof(o); } ; "                                               ;~ Get JSON obj type method 2 - recommended usage

    $JSON_COM_extentions = $JSON_COM_extentions & $JSON_path_framework & $JSON_framework

    ; Strings and Numbers
    ; This is only useful if you want to access Javascript string and number functions.
    Local $LiteralObj = "" & _
            "Object.prototype.dot                       = function( str, jsStrFun ) { " & _
            "   if ( typeof str == 'string' )  { return eval( '""'+  protectDoubleQuotes (str)  + '"".' + jsStrFun ) " & _
            "       } else { return eval( ''+  str  + '.' + jsStrFun ) } " & _
            "} ; " & _
            "function oLiteral ( literal ) { " & _
            "  this.literal = literal; " & _
            "} " & _
            "function protectDoubleQuotes (str)   { " & _
            "   return str.replace(/\\/g, '\\\\').replace(/""/g,'\\""');   " & _
            "}" & _
            "Object.prototype.toObj                 = function( literal ) { " & _
            "   if ( typeof literal == 'string' )  { return eval( 'new oLiteral(""' + protectDoubleQuotes (literal) + '"")' ) " & _
            "       } else { return eval( 'new oLiteral(' + literal + ')' ) } " & _
            "} ; " & _
            "Object.prototype.jsMethAdd = function( funname , numParams ) { " & _
            "   var x = buildParamlist (numParams)  ;  " & _
            "   return eval( 'oLiteral.prototype.' + funname + ' = function(' + x + ') { return this.literal.' + funname + '(' + x + '); }' ); " & _
            "} ; "

    ; OO framework for basic OO and JSON to autoIT -
    Return "" & _
            $OO_extra & _
            $OO_js_native & _
            $OO_extend_framework & _
            $JSON_COM_extentions & _
            $LiteralObj
EndFunc   ;==>_OO_JSON_SET_Framework
;~ ____________________________________________________________________________
Func _OO_JSON_SET_HTML()
    Return "<!DOCTYPE html>  " & _
            "<head>" & _
            "<script>" & _
            $g_OO_JSON_JS_framework & _
            "</script>" & _
            "</head>" & _
            "<body>" & _
            "</body>" & _
            "</html>"
EndFunc   ;==>_OO_JSON_SET_HTML
;~ ____________________________________________________________________________
Func _OO_JSON_Read_File($filename = "jsonpath-0.8.0.js")
    Local $file = FileOpen($filename, 0)

    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    ; Read in 1 character at a time until the EOF is reached
    Local $chars = FileRead($file) ; , 1)
    FileClose($file)
    Return $chars
EndFunc   ;==>_OO_JSON_Read_File
;~ ____________________________________________________________________________
Func _OO_JSON_Init()
    ; see also _OO_JSON_custom_init
    $g_OO_JSON_non_IE = ObjCreate("ScriptControl")
    $g_OO_JSON_non_IE.language = "Jscript"
    $g_OO_JSON_non_IE.AddCode($g_OO_JSON_JS_framework)
    Return _OO_JSON_JS_Obj_Create().objGet("JSON") ; now we are OO!
EndFunc   ;==>_OO_JSON_Init
;~ ____________________________________________________________________________
Func _OO_JSON_JS_Obj_Create($def = "{}")
    $g_OO_JSON_non_IE.Eval("var z " & "= " & $def)
    Return $g_OO_JSON_non_IE.Eval("z")
EndFunc   ;==>_OO_JSON_JS_Obj_Create
;~ ____________________________________________________________________________
Func _OO_JSON_Quit()

EndFunc   ;==>_OO_JSON_Quit
;~ ____________________________________________________________________________

 

Edited by Page2PagePro

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
×
×
  • Create New...