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
Remove All Unnecessary Whitespaces from config String with regex
Remove All Unnecessary Whitespaces from config String with regex
Last edited by SL5 on 12 Jan 2019, 15:57, edited 1 time in total.
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Remove All Unnecessary Whitespaces from JSON String with regex
I think it's not so easy as I first thought:
discussion is started here: https://stackoverflow.com/questions/541 ... autohotkey
discussion is started here: https://stackoverflow.com/questions/541 ... autohotkey
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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
}
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Remove All Unnecessary Whitespaces from JSON String with regex
This is not valid JSON. All the keys must be enclosed in quotes:SL5 wrote: ↑11 Jan 2019, 12:13Code: Select all
configJSONContent = ( g_config := { FuzzySearch:{ enable: true , keysMAXperEntry : 6 , doValueCopy : false , halloWorld : "Hello World 3" } } )
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.
-
- Posts: 440
- Joined: 28 Apr 2018, 21:59
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Remove All Unnecessary Whitespaces from JSON String with regex
AutoHotkey array is not the same as JSON. The question is about JSON, isn't it?MannyKSoSo wrote: ↑11 Jan 2019, 12:34You 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
-
- Posts: 440
- Joined: 28 Apr 2018, 21:59
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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.
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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.
same problem here:
its producing:
test := { ddd : "4" }
but messagebox is empty
UPDATE: I found out thats is script dependent.
it works in a simple test script.
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.
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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...
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...
Re: Remove All Unnecessary Whitespaces from JSON String with regex
thankssafetycar wrote: ↑11 Jan 2019, 13:03Personally 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...
I know only this one to assign JSON to a variable in autohotkey. if i know it better i do it
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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} }
You'll be mostly using colon ":" instead of assignments.
For example: { key1:val1, key2 : { key3:val3} }
Re: Remove All Unnecessary Whitespaces from JSON String with regex
yes. i will rename my vars an function.safetycar wrote: ↑11 Jan 2019, 13:16To 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} }
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" ){
..
i like that ( https://github.com/sl5net/global-Intell ... ig.inc.ahk )
creates that:
Last edited by SL5 on 12 Jan 2019, 15:58, edited 2 times in total.
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Remove All Unnecessary Whitespaces from JSON String with regex
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.
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.
Re: Remove All Unnecessary Whitespaces from JSON String with regex
yes thank I have mentioned elsewhere down, that I was mistaken with the title. how can you change that(solved: i changed first posting title)?safetycar wrote: ↑12 Jan 2019, 02:33I 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.
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]
github>g_IntelliSense + next improvements + forum
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk..org>onlineAHKprettyfy, ahk..com>Refactoring
ahk..com>newposts Unanswrd myposts, Donations are appreciated if I could help you
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA