Remove All Unnecessary Whitespaces from config String with regex

Get help with using AutoHotkey and its commands and hotkeys
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Remove All Unnecessary Whitespaces from config String with regex

11 Jan 2019, 10:26

hwoto Remove All Unnecessary Whitespaces from JSON String with regex?

certainly very easy to do.

but I still ask maybe that's already exist here
Last edited by SL5 on 12 Jan 2019, 15:57, edited 1 time in total.
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 11:07

I think it's not so easy as I first thought:

discussion is started here: https://stackoverflow.com/questions/541 ... autohotkey
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:13

best result for the moment. works for me (have no so difficlut values for the moment).

Code: Select all

configJSONContent =
(
	g_config := 
	{ 
        FuzzySearch:{
            enable: true ,
            keysMAXperEntry : 6 ,
            doValueCopy : false , 
		  halloWorld : "Hello World 4"
        }
    }
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include *i %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % g_config["FuzzySearch"]["halloWorld"]
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*" )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,50
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}

teadrinker
Posts: 1018
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:33

SL5 wrote:
11 Jan 2019, 12:13

Code: Select all

configJSONContent =
(
	g_config := 
	{ 
        FuzzySearch:{
            enable: true ,
            keysMAXperEntry : 6 ,
            doValueCopy : false , 
		  halloWorld : "Hello World 3"
        }
    }
)
This is not valid JSON. All the keys must be enclosed in quotes:

Code: Select all

json =
(
   { 
        "FuzzySearch":{
            "enable": true ,
            "keysMAXperEntry" : 6 ,
            "doValueCopy" : false , 
            "halloWorld" : "Hello World 3"
        }
    }
)
oDoc := ComObjCreate("htmlfile")
oDoc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">")
try oDoc.parentWindow.eval("var test = JSON.stringify(JSON.parse('" . RegExReplace(json, "\R") . "'));")
catch {
   MsgBox, Invalid json!
   Return
}
MsgBox, % oDoc.parentWindow.test
Last edited by teadrinker on 11 Jan 2019, 12:36, edited 1 time in total.
MannyKSoSo
Posts: 434
Joined: 28 Apr 2018, 21:59

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:34

You are correct it is not a valid JSON, but you can see that he is making it an array that he can use inside of autohotkey
teadrinker
Posts: 1018
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:44

MannyKSoSo wrote:
11 Jan 2019, 12:34
You are correct it is not a valid JSON, but you can see that he is making it an array that he can use inside of autohotkey
AutoHotkey array is not the same as JSON. The question is about JSON, isn't it? :)
MannyKSoSo
Posts: 434
Joined: 28 Apr 2018, 21:59

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:56

Indeed it is different than JSON, but looking at the code he provided he is just using the style of the JSON (no quotes except in the data section), but uses it as a variable to call the information, which is why its not proper JSON format. So its kinda a mixture of two things.
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 12:58

but for some reason that does not work (its empty):

UPDATE: I found out thats is script dependent.
it works in a simple test script.
2019-01-11 19_17_59-global-IntelliSense-everywhere-Nightly-Build [G__fre_git_github_global-IntelliSe.png
2019-01-11 19_17_59-global-IntelliSense-everywhere-Nightly-Build [G__fre_git_github_global-IntelliSe.png (7.14 KiB) Viewed 1169 times

Code: Select all

configJSONContent =
(
test :=
 {
   enable: "true",
   MAXlines: "87654",
   keysMAXperEntry : "6",
   minKeysLen: "4",
   doValueCopy: "false"
 } ; difficult to implement symlink copy for not rr lines doValueCopy. todo: issue . doValueCopy : false  is not fully implemented
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % "minKeysLen=" test["FuzzySearch"]["minKeysLen"]
FileRead, configJSONContentminify, %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % configJSONContentminify
reload
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	; Changes always become active on the next next call. becouse include is a preparser command. 19-01-11_18-33
	; discussion here: https://stackoverflow.com/questions/54149980/remove-all-unnecessary-whitespaces-from-json-string-with-regex-in-autohotkey
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*" )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,40
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}


same problem here:

its producing:
test := { ddd : "4" }

but messagebox is empty

Code: Select all

test := {}
configJSONContent =
(
test :=
{
   ddd : "4"
}
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include %A_ScriptDir%\configJSON.minify.inc.ahk
FileRead, configJSONContentminify, %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % "test[ddd]=" test["ddd"] "`n`n" configJSONContentminify
; MsgBox, % configJSONContentminify
reload
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	; Changes always become active on the next next call. becouse include is a preparser command. 19-01-11_18-33
	; discussion here: https://stackoverflow.com/questions/54149980/remove-all-unnecessary-whitespaces-from-json-string-with-regex-in-autohotkey
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*", " " )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,40
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}
Last edited by SL5 on 11 Jan 2019, 13:18, edited 3 times in total.
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 13:03

Personally I don't think it's a good Idea mixing things.
If you want to make a valid AHK config I'd go with an include on top of your script.
If you want to make a valid JSON, then don't use the ":=" operator or other syntax that isn't JS valid.
Maybe an <AHKON> could be possible but I see it as quite some effort...
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 13:08

safetycar wrote:
11 Jan 2019, 13:03
Personally I don't think it's a good Idea mixing things.
If you want to make a valid AHK config I'd go with an include on top of your script.
If you want to make a valid JSON, then don't use the ":=" operator or other syntax that isn't JS valid.
Maybe an <AHKON> could be possible but I see it as quite some effort...
thanks
I know only this one to assign JSON to a variable in autohotkey. if i know it better i do it :)
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 13:16

To keep it simple JSON it's mostly about putting objects inside objects, you're not defining variables in JSON, you're just describing object keys and values.
You'll be mostly using colon ":" instead of assignments.
For example: { key1:val1, key2 : { key3:val3} }
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

11 Jan 2019, 13:59

safetycar wrote:
11 Jan 2019, 13:16
To keep it simple JSON it's mostly about putting objects inside objects, you're not defining variables in JSON, you're just describing object keys and values.
You'll be mostly using colon ":" instead of assignments.
For example: { key1:val1, key2 : { key3:val3} }
yes. i will rename my vars an function.

i want a structured json like, nearly json like, config possibililty into a seperate file, with the possibilty to layout it with newlines and tabs.
probably the best from doku: https://www.autohotkey.com/docs/Objects ... ive_Arrays

updated:

Code: Select all

configStr2minify_configFile(configIncAhkAddress := "\config\config.inc.ahk", configMinifyIncAhkAddress := "\config.minify.inc.ahk" ){
..
now its permantly updated here: https://github.com/sl5net/global-Intell ... nc.ahk#L19

i like that :)
2019-01-11 21_05_19-Table awaiting Action.png
2019-01-11 21_05_19-Table awaiting Action.png (23.35 KiB) Viewed 1105 times
( https://github.com/sl5net/global-Intell ... ig.inc.ahk )
creates that:
2019-01-11 22_31_43-Table awaiting Action.png
2019-01-11 22_31_43-Table awaiting Action.png (11.06 KiB) Viewed 1102 times
Last edited by SL5 on 12 Jan 2019, 15:58, edited 2 times in total.
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Remove All Unnecessary Whitespaces from JSON String with regex

12 Jan 2019, 02:33

I think what you're trying to do is ok from the point of view of functionality (I haven't seen where the g_config is set to {} but I guess it's somewhere or it won't work).

But don't get confused, what you're doing is not JSON. Because JSON is about an Object represented as a String. If you don't have an Stringifier and a Parser(Destringifier) you're just working with objects directly. Which is not bad, but it's good to know the difference.

Or maybe I got the wrong idea because I haven't seen any reference to those functions.
User avatar
SL5
Posts: 861
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Remove All Unnecessary Whitespaces from JSON String with regex

12 Jan 2019, 06:05

safetycar wrote:
12 Jan 2019, 02:33
I think what you're trying to do is ok from the point of view of functionality (I haven't seen where the g_config is set to {} but I guess it's somewhere or it won't work).

But don't get confused, what you're doing is not JSON. Because JSON is about an Object represented as a String. If you don't have an Stringifier and a Parser(Destringifier) you're just working with objects directly. Which is not bad, but it's good to know the difference.

Or maybe I got the wrong idea because I haven't seen any reference to those functions.
yes thank :thumbup: I have mentioned elsewhere down, that I was mistaken with the title. how can you change that(solved: i changed first posting title)? :problem:
this is an associative array

yes g_config is set to {} is done inside the script. no prob. Thank you, I want to mention that in the documentary:
[code]
useItLike =
(
SetTimer,check_configFile_Changed,2000
g_config := {}
configStr2minify_configFile()
# Include *i %A_ScriptDir%\config.minify.inc.ahk
)

[/code]

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Bort P, ElektroBerry, flint, Google [Bot], Gully, KHA, mehrez1989, SuperFoobar and 196 guests