json encode, for http post and otherwise

newfish

New member
This started out as fiddling with getting newlines encoded for a HTTP POST, but it turns out that's not solving the issue. As soon as other things that json needs escaped wind up in your request, it's borked all over again.

A solution would be: have a checkbox that allows input data to a POST to be json "stringified" (as javascript calls it).

Another angle would be to have the reverse of the json parse action that already exists - a json encode action - then one could just preprocess and then create the POST, and the encoder could be useful elsewhere too. Lots of things talk in json...

Anyhow thanks so much for macrodroid!
 

Endercraft

Moderator (& bug finder :D)
This started out as fiddling with getting newlines encoded for a HTTP POST, but it turns out that's not solving the issue. As soon as other things that json needs escaped wind up in your request, it's borked all over again.

A solution would be: have a checkbox that allows input data to a POST to be json "stringified" (as javascript calls it).

Another angle would be to have the reverse of the json parse action that already exists - a json encode action - then one could just preprocess and then create the POST, and the encoder could be useful elsewhere too. Lots of things talk in json...

Anyhow thanks so much for macrodroid!
This action was added recently, you just have to update to v5.29
 
Last edited:

FrameXX

Well-known member
You can download the update here:
 

newfish

New member
You can download the update...
Very exciting, although unfortunately silently fails to do anything, no error in logs and no output to dictionary. I hand it a string with multiple lines, single and double quotes and parentheses in it though...perhaps it works for .."simpler" uses? Or I'm not understanding it's purpose?
 

Endercraft

Moderator (& bug finder :D)
Very exciting, although unfortunately silently fails to do anything, no error in logs and no output to dictionary. I hand it a string with multiple lines, single and double quotes and parentheses in it though...perhaps it works for .."simpler" uses? Or I'm not understanding it's purpose?
JSON Output ? No, no, it converts dictionaries/arrays into a string and JSON Parse does the opposite.
 

newfish

New member
Best solution Ive found so far is to use the File action to write data into a file termux can access, then using python json.dumps...then saving that to a text file and having the POST action read from that. Passing escaped stuff into a macrodroid var will make the request fail.
I understand that 'behind the scenes' macrodroid keeps the scripts themselves in json...i could see how this could prevent/mess up having escaped json inside of json. or idk that was my guess.
 

FrameXX

Well-known member
It works for me. You are probably doing something wrong. You need to give it MacroDroid dictionary variable and it will output you a stringified version of the dictionary in form of MacroDroid string variable.
 

Endercraft

Moderator (& bug finder :D)
We're not understanding eachother. I need to insert random code snippets _into_ a json string, not extract strings fom json format
Can you use text manipulation - > replace all then ?

Another option is to Parse, add some variables and Output. I just made a macro that works like that.
 

newfish

New member
Can you use text manipulation - > replace all then ?

Another option is to Parse, add some variables and Output. I just made a macro that works like that.
There's something about the \n escape in particular that macrodroid barfs on. Adding \ to some others was working ok, but it would just continually revert any \n to a physical newline in the string.
 

newfish

New member
using 'from file' in HTTP POST worked fine twice, and then it started erroring. Not sure why. Have messed with file permissions in every available way and I can't get it to work anymore.

"java.lang.SecurityException: Permission Denial: reading com.termux.filepicker.TermuxDocumentsProvider uri content://com.termux.documents/document/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Ft0 from pid=21887, uid=10347 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs"

EDIT:
...a fix to that is to use the termux-storage hack and then point macrodroid and termux to do their file I/O in downloads, instead of inside termux's virtual 'home'.

Edit again:
Nope, that also works once or twice and then somehow the permission to access gets quietly revoked. :/
 
Last edited:

newfish

New member
That's because it's a MacroDroid feature... I'm not sure how to stop this.
im trying to send unadulterated clipboard contents to a http api. the path is
clipboard -> file -> python (to json-escape) -> file -> md post request
if the text gets stored in any macrodroid var it becomes unreadable by the remote json parser.
 

newfish

New member
I wonder if there could be an option in all text fields to use plain text.
That would be great...there's still the desire to send (arbitrary) data via a POST, which expects json or urlencoded, and no way to accomplish either of those conversions in MD...that was the feature request I was trying to make
 
Top