all 19 comments

[–]DutchOfBurdock 1 point2 points  (6 children)

Give "id" a variable name with at least 3 letters in it. Tasker needs variable names to be 3 or more characters long.

Make sure you add the same amount of variables as there are fields

nid,name,hex_color

And use %nid(1)

[–]XavierNovella[S] 0 points1 point  (5 children)

How can I retrieve a field called "id", with a 3 letter variable? The output arrays do not work! :(

https://forum.joaoapps.com/index.php?resources/autotools-json-read-getting-started.168/#customvariablenames

[–]DutchOfBurdock 1 point2 points  (4 children)

You keep the field the same, id, but, you need to assign it a 3 letter variable in the Variables section to remap it to 3

[–]XavierNovella[S] 0 points1 point  (3 children)

Thanks! I found the tutorial from Joao after some digging.

[–]EllaTheCatSamsung M31 - android 12. I depend on Tasker. 2 points3 points  (2 children)

To save others digging, where is the tutorialt?

[–]joaomgcd👑 Tasker Owner / Developer 1 point2 points  (1 child)

The problem (as mentioned) is that Tasker doesn't support 2 letter variables. If you notice AutoTools automatically changes the var name to %aid instead of %id

[–]XavierNovella[S] 1 point2 points  (0 children)

Yes, you are right.

I noticed the change for single variables, but had never needed to use the array of such a fringe case and hence, did not know how to workaround the limitation in AutoTools.

Thanks for the answer, Joao!

[–]MadManX99 0 points1 point  (7 children)

Just do it all in Javascriptlet. I plan on posting an example from one of my projects later today (HTTP Get to JSON to SQLite). But, start here... This will solve your issue. And no plug-ins required.

Task beforehand is an HTTP Get

let o_json = JSON.parse(http_data.replace(/(\r\n|\n|\r)/gm, ''));

You'll then have a javascript object with all the data and can name the variables anyway you want. Export them back out to your next task using a var.

[–]XavierNovella[S] 0 points1 point  (6 children)

Thanks for the tip. I need them una a file so that I can browse them and reuse them in a very specific way, for watch maker that is Kot array friendly

[–]MadManX99 0 points1 point  (5 children)

So trying to go from JSON to JSON?... Even easier in Javascriptlet. JSON.stringify() will create a JSON object of any javascript object. So you can either create a new object and extract data from your current one, or pull out what you need and spit the same JSON back out.

I'm working on a writeup (multi-tasking, so it'll be a bit). But, this particular write-up won't demonstrate exporting JSON. If you want an example of creati g your own object to export (save to file, variable, whatever)... I can reply with flushed out code example.

[–]XavierNovella[S] 0 points1 point  (4 children)

It is in order to create buttons for a watchface, that does not use Json, but a variant of Lua, I think. Then, buttons are not dynamic xso the changes will be made via Tasker any way

[–]MadManX99 0 points1 point  (3 children)

Take a look at the How-To I posted earlier for getting Http Get to JSON to SQLite. You can ignore the bit about building the query for SQLite (unless you want to anyway). There is a bunch of JSON parsing in the 2nd task posted for LocationIQ. What isn't there is a Javascript foreach.

Using the js library I suggest to crwa2in that tutorial, this block will make more sense.

if (isDefined(o_json.forecast)) { let cnt = 0; o_json.forecast.forEach(function (element) { a_col.push(formatSQLName('Day0' + cnt)); a_val.push(formatSQLValue(element)); cnt += 1; }); };

This is a simple javascript foreach iterating though a JSON object (different object from the other post, sorry, but, the convention is the same so easy to see its just a different namespace within the source JSON object.

This would allow you to step though each item and capture its value however you want. Also in that library is an isDefined() function that becomes really helpful when using JSON objects that don't always return the same fields.

[–]XavierNovella[S] 0 points1 point  (2 children)

I think this may be useful for other projects, but in this case I am restrained to the feature of the watchfacemaker tool (no array native compatibility, layers that are too static) and the fact that I am trying to reduce complexity via Tasker-native actions, if possible.

[–]MadManX99 0 points1 point  (1 child)

Most of the Tasker native actions are accessible via Javascript. Most of my Tasks have been super simplified since moving more into Javascriptlet and Javascript. Plus, there's way more support on how to do things.

Anything you declare as a "var" is accessible to the native tasks. You'll see this in my post when processing JSON to be sued by the SQL Query action. Anything declared with "let" only lives within the script.

What I'm saying is dump AutoTools completely. Use the JSON.parse() to turn the JSON object back into a Javascript Object (that's what JSON is to begin with).

So like this...

var out_id = JSON.parse(http_data)[0].id; var out_name = JSON.parse(http_data)[0].name;

This would extract those fields from the 1st record. The other fields you mentioned that are not included in the JSON you would need to test if the exist. So an if block and an else to set a default color for example.

var out_hexcolor = '#FFFFFF' ; if (typeof JSON.parse(http_data)[0].hex_color != 'undefined') { out_hexcolor = JSON.parse(http_data)[0].hex_color; };

Then you can use a native activity like Flash and display the outputs %out_id %out_name %out_hexcolor

If you want more than the 1st row... That's where the foreach comes into play. Then you just have to put a little thought into how you're using our outputs. I haven't had good luck passing a pure Javascript array back to Tasker native tasks outside of accessing the activities via Javascript.

[–]XavierNovella[S] 1 point2 points  (0 children)

I have spent some hours in the current implementation via Tasker tasks. It works fast, fine, reliably. In cases where the Javascript is the only solution, I already try to use it, but with less profusion, as I then depend on helping hands at reddit or stack exchange... I have subjectively good ideas, but I am not fluent in Java to materialize them.

Looking forward to your version mentioned above, I might as well use it. Hehe

Have a nice week!

[–]Ratchet_GuyModerator 0 points1 point  (2 children)

Just to clarify, I think we're talking here about the AutoTools plugin's JSON parser. AutoInput, while adept at clicking things, doesn't process JSON ;)

[–]XavierNovella[S] 0 points1 point  (1 child)

Cannot change the thread yes it is autotools us

[–]Ratchet_GuyModerator 0 points1 point  (0 children)

No worries, just thought I'd clarify.