JavaScriptlet parsing JSON issues

519 views
Skip to first unread message

mechanimal82

unread,
Nov 17, 2014, 9:57:49 AM11/17/14
to tas...@googlegroups.com
Hi, sorry I'm a n00b and have been trying to figure this one out but it's just eluding me. I've searched and tried what I can here and on Google but for some reason I just can't seem to set a global variable to be used out of a web query JSON:

I'm trying to query the Spotify api for album availability; for example: https://api.spotify.com/v1/albums/2nsRpsb5aeOhNEWV1MoiHB

I then want to parse the resulting %HTTPD response to set a variable with the 'available_markets'... among others, but lets start here.


What I have so far is the HTTP Get which gets the correct result (as shown in the link provided); I'm then trying the below, which runs OK but no variable is created:

var result = JSON.parse(global("HTTPD"));
setGlobal("Name",name);
setGlobal("Country",available_markets);


like I say it runs, but I have no variables to play with. I also tried 'flash(result.length)' which returns 'undefined'

if however I run 'flash(global("HTTPD"));' on line 2 it will flash the result of the HTTP Get.


Please help a noob... thanks anyone for this!


Sean Williams

unread,
Nov 17, 2014, 11:48:54 AM11/17/14
to tas...@googlegroups.com
It looks like its probably your syntax. Use ' instead of " so for example,

For vars

setGlobal('Name',name);

Or strings

setGlobal('Name',"name");

mechanimal82

unread,
Nov 17, 2014, 12:06:00 PM11/17/14
to tas...@googlegroups.com
Thanks, just tried both options and still nothing shows.... Confused.

Manu Pinazo

unread,
Nov 18, 2014, 4:52:50 AM11/18/14
to tas...@googlegroups.com
You are storing parsed object in result, so the "name" should be accessed as result.name, and so on.
Also note that "available_markets" is an array, you could store it as a comma-separated string so you can split it later.

Your example could be like this:


var result = JSON.parse(global("HTTPD"));
setGlobal("Name",result.name);
setGlobal("Country",result.available_markets.join(','));

mechanimal82

unread,
Nov 18, 2014, 5:45:46 PM11/18/14
to tas...@googlegroups.com
Thanks for your help so far. I however seem to be getting undefined returned in any variable. I'm beginning to wonder if I'm missing a setting in Tasker or something.

Sean Williams

unread,
Nov 19, 2014, 1:55:30 AM11/19/14
to tas...@googlegroups.com
As Manu wrote make sure you reference your object


var result = JSON.parse(local('input'));

setLocal('name',result.name);

setLocal('country',result.available_markets.join(','));

flashLong( local('name')+local('country'));

Make sure your input is correctly set, try using local variables.

So for example Set local, %input to %HTTPD before you run the scriptlet and if needed add some extra toasts in your scriplet to see what's happening.

If its still failing maybe post your XML so we can take a look.

mechanimal82

unread,
Nov 19, 2014, 6:29:26 PM11/19/14
to tas...@googlegroups.com
Thanks so much. For some reason this is still getting the better of me. Here's my task as an xml. I was trying to start simply and then build on this, hopefully after this initial hurdle I'll get there.


<TaskerData sr="" dvi="1" tv="4.6b3m">
<Task sr="task165">
<cdate>1416137070215</cdate>
<edate>1416439552656</edate>
<id>165</id>
<nme>WebAPI</nme>
<pri>10</pri>
<Action sr="act0" ve="7">
<code>118</code>
<Str sr="arg0" ve="3">https://api.spotify.com</Str>
<Str sr="arg1" ve="3">v1/search</Str>
<Str sr="arg2" ve="3">q=drive
type=track</Str>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
<Int sr="arg5" val="10"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Int sr="arg8" val="0"/>
</Action>
<Action sr="act1" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%Results</Str>
<Str sr="arg1" ve="3">%HTTPD</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act2" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%input</Str>
<Str sr="arg1" ve="3">%HTTPD</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act3" ve="7">
<code>129</code>
<Str sr="arg0" ve="3">var result = JSON.parse(local('input'));

setLocal('name',result.name);

setLocal('country',result.available_markets.join(','));

flashLong( local('name')+local('country'));</Str>
<Str sr="arg1" ve="3"/>
<Int sr="arg2" val="1"/>
<Int sr="arg3" val="45"/>
</Action>
<Action sr="act4" ve="7">
<code>548</code>
<Str sr="arg0" ve="3">%country</Str>
<Int sr="arg1" val="0"/>
</Action>
</Task>
</TaskerData>

Sean Williams

unread,
Nov 20, 2014, 4:14:52 AM11/20/14
to tas...@googlegroups.com
I can't import your task as I'm on an older version of tasker than you but looking at your XML it is something to do with the data from your tasks api request I don't know Spotify api so you will need to look into this.

However your original post includes an id in its request where as the request in your XML does not.

Here's an example using your original post.


<TaskerData sr="" dvi="1" tv="4.5u1m">
<Task sr="task303">
<cdate>1416137070215</cdate>
<edate>1416473302151</edate>
<id>303</id>
<nme>Sportify</nme>
<pri>10</pri>
<Action sr="act0" ve="7">
<code>118</code>
<se>false</se>
<Str sr="arg0" ve="3">https://api.spotify.com</Str>
<Str sr="arg1" ve="3">v1/albums/2nsRpsb5aeOhNEWV1MoiHB</Str>
<Str sr="arg2" ve="3"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
<Int sr="arg5" val="1"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Int sr="arg8" val="1"/>
</Action>
<Action sr="act1" ve="7">
<code>547</code>
<Str sr="arg0" ve="3">%input</Str>
<Str sr="arg1" ve="3">%HTTPD</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act2" ve="7">
<code>129</code>
<Str sr="arg0" ve="3">var result = JSON.parse(local('input'));

setLocal('name',result.name);

setLocal('country',result.available_markets.join(','));

flashLong(local('name')+local('country'));</Str>
<Str sr="arg1" ve="3"/>
<Int sr="arg2" val="1"/>
<Int sr="arg3" val="1"/>
</Action>
</Task>
</TaskerData>
Reply all
Reply to author
Forward
0 new messages