Page 1 of 1
JSON
Posted: Thu Jun 05, 2014 7:33 pm
by UtahCode197
Hi,
I want to use a JSON link in a program. It's basically a currency converter and the rate comes from a JSON link. How can I get it so that when the figures change in the JSON a variable in live code updates? I've seen some other posts from other users about the JSON topic but I don't understand them or they're to what I'm looking for.
Thanks

Re: JSON
Posted: Thu Jun 05, 2014 8:23 pm
by sefrojones
I woked on something similar, that grabbed market info about cryptocurrencies from Cryptsy.com API. I just threw together a small example of how I got the information for the BTC/LTC market on cryptsy parsed into my app. I am not an expert in JSON, but this method worked for me. here's a SS of the attached example:
Hopefully this helps.
--Sefro
edit:
As far as having it update a variable, in my app i just grab the info at a predetermined interval (ie 30 seconds) and then act accordingly.
DELETED EXAMPLE as arrays seem to be better suited for this type of thing.
Re: JSON
Posted: Thu Jun 05, 2014 9:25 pm
by bangkok
RoryJMcEwan wrote:Hi,
I want to use a JSON link in a program. It's basically a currency converter and the rate comes from a JSON link. How can I get it so that when the figures change in the JSON a variable in live code updates? I've seen some other posts from other users about the JSON topic but I don't understand them or they're to what I'm looking for.
Could you give us more details about this "JSON link" ?
JSON is just a format for data, like XML.
Look at some example here :
http://json.org/example
So basically, if I understand what you want to achieve :
-you send a query to a webservice
-the webservice answers with data in JSON format
-LiveCode decodes the JSON format and displays an exchange rate for example
-plus you want LiveCode to automatically send the query, let's say every hour ?
--> to send the query, get the address of the webservice and the format of the data it is waiting for.
Usually, it's a POST query.
Like :
Code: Select all
put "rate=EURUSD" into tData
POST tData to url "http://www.rate.com/webservice/exchangerate"
put it into tResult
--> then after usually we need to transform the received JSON data into an array.
For this task you can use the very good library EasyJSON, here :
https://github.com/luxlogica/easyjson
With a simple command :
Code: Select all
put arrayFromJson(tResult) into tArray
Then you can explore the content of your array, and its keys.
Last but not least : if the data you receive are very simple, and if you dont want to bother with JSON library and arrays, you could perhaps parse directly the text received (thanks to the text crunching capabilities of Livecode).
Re: JSON
Posted: Fri Jun 06, 2014 12:45 am
by sefrojones
bangkok's solution is probably the right one for the job. I have no experience with arrays, so I tried out the EasyJSON library recommended along with a function to display an array in a human readable form from this lesson :
http://lessons.runrev.com/m/4071/l/2102 ... dable-form
I've been able to take the JSON that I get from cryptsy, and put it into an array with EasyJSON's arrayFromJSON function, as well as display the array with the displayArrayData function from the lesson above. My problem is trying to extract specific information from the array, I can't figure out, for example, how I would grab the [recenttrades] or even better each result below(inside?) it: [35],[36], etc.. and display it in it's own field. When I try to put the keys, I get success and return, and when I try to list the elements, I get "1" I've also tried to combine using return, but that also just returns a "1"
Here's a pic of what I have:
I'll attach this stack if anyone is willing to look at it, or point me in the right direction as far as pulling these bits of data out of this array I would appreciate it. I've been looking through all of the runrev lessons involving arrays, but I'm a bit lost..... Thanks!
--Sefro
Re: JSON
Posted: Fri Jun 06, 2014 8:13 am
by bangkok
sefrojonesGAda40 wrote: My problem is trying to extract specific information from the array, I can't figure out, for example, how I would grab the [recenttrades] or even better each result below(inside?) it: [35],[36], etc.. and display it in it's own field.
It's like a tree.
In your script, put a breakpoint at
Code: Select all
put displayArrayData(tArray,".") into field "ArrayView"
Then, look at the content of tArray on the left pane... You can go through the levels of the Array.
So to answer your question, for instance :
Code: Select all
answer tArray["return"]["markets"]["LTC"]["buyorders"][1]["price"]
Re: JSON
Posted: Fri Jun 06, 2014 3:24 pm
by sefrojones
Thanks bangkok! I think I've got an idea of how to work with arrays now. I saw that it was like a tree, it was the syntax that was eluding me. Thanks again!
edit: This is a lot easier than my first example where I parsed the result in LiveCode, I assume it's also more efficient.
Re: JSON
Posted: Fri Jun 06, 2014 4:48 pm
by UtahCode197
bangkok wrote:RoryJMcEwan wrote:Hi,
I want to use a JSON link in a program. It's basically a currency converter and the rate comes from a JSON link. How can I get it so that when the figures change in the JSON a variable in live code updates? I've seen some other posts from other users about the JSON topic but I don't understand them or they're to what I'm looking for.
Could you give us more details about this "JSON link" ?
JSON is just a format for data, like XML.
Look at some example here :
http://json.org/example
So basically, if I understand what you want to achieve :
-you send a query to a webservice
-the webservice answers with data in JSON format
-LiveCode decodes the JSON format and displays an exchange rate for example
-plus you want LiveCode to automatically send the query, let's say every hour ?
--> to send the query, get the address of the webservice and the format of the data it is waiting for.
Usually, it's a POST query.
Like :
Code: Select all
put "rate=EURUSD" into tData
POST tData to url "http://www.rate.com/webservice/exchangerate"
put it into tResult
--> then after usually we need to transform the received JSON data into an array.
For this task you can use the very good library EasyJSON, here :
https://github.com/luxlogica/easyjson
With a simple command :
Code: Select all
put arrayFromJson(tResult) into tArray
Then you can explore the content of your array, and its keys.
Last but not least : if the data you receive are very simple, and if you dont want to bother with JSON library and arrays, you could perhaps parse directly the text received (thanks to the text crunching capabilities of Livecode).
Thanks for your reply!
So far this is the code for a button which I want to take the rate (GBP) from
https://bitpay.com/api/rates
Code: Select all
on mouseup
put rate = GBP into tData
POST tData to url "https://bitpay.com/api/rates"
put it into tResult
put it into bitcoin
end mouseup
The code returns no error but it doesn't show up in the 'bitcoin' field that I'm trying to get it to.
Can you see what I'm doing wrong??
Thanks so far

Re: JSON
Posted: Fri Jun 06, 2014 5:37 pm
by sefrojones
In this example I'm just grabbing the whole list of rates from bitpay putting the JSON into a LC array and then pulling out the relevant bits to put into the relevant fields. I also included the tree view. I commented the "Get JSON market data" button to hopefully explain this a bit further. I just Figured it out this morning thanks to bangkok! But here's a simple example of what you're asking:
I hope this helps. Thanks to bangkok, I now understand a bit more about arrays and how to use them!
--Sefro
Re: JSON
Posted: Fri Jun 06, 2014 5:47 pm
by sefrojones
I checked out bitpay site a little bit, and i found this :
https://bitpay.com/api/rates/gbp
so this example just grabs that instead of the whole list of currencies/rates

Re: JSON
Posted: Fri Jun 06, 2014 7:09 pm
by UtahCode197
Hi,
It's working slightly better now but I still can't get the rate into a label object.
The code for the button to get the rate is:
Code: Select all
on mouseUp
//get your data
put "https://bitpay.com/api/rates/GBP" into field tMarketURL
get url tMarketURL
put "rate" into Field "JSON"
//put it into field "JSON"
//put it into tMarketJSON //put it into a variable
//I copied the EasyJSON libarary into the stack script, this converts your
//raw JSON into a livecode array
//put arrayFromJSON( tMarketJSON) into tArray
//Now we pull the relevant data to our fields
//put tArray[3]["name"] into field "name"
//put tArray[3]["code"] into field "code"
put tArray[3]["rate"] into field "JSON"
end mouseUp
It still returns no errors but the rate doesn't show up in the feed.
Thanks
Re: JSON
Posted: Fri Jun 06, 2014 7:11 pm
by sefrojones
try this:
Code: Select all
put tArray["rate"] into field "JSON"
Re: JSON
Posted: Fri Jun 06, 2014 7:11 pm
by sefrojones
yet another double post. I'm not sure how that's happening......
On another note, if you take alook at the 2nd example that only deals with GBP, i think you'll see why your script wasn't working.
Code: Select all
put "https://bitpay.com/api/rates/GBP" into tMarketURL
get url tMarketURL
put it into tMarketJSON
put arrayFromJSON(tMarketJSON) into tArray
put tArray["rate"] into field "rate"
this should work to put the rate into a fld named "rate".....
Re: JSON
Posted: Sat Jun 07, 2014 1:47 pm
by UtahCode197
sefrojonesGAda40 wrote:yet another double post. I'm not sure how that's happening......
On another note, if you take alook at the 2nd example that only deals with GBP, i think you'll see why your script wasn't working.
Code: Select all
put "https://bitpay.com/api/rates/GBP" into tMarketURL
get url tMarketURL
put it into tMarketJSON
put arrayFromJSON(tMarketJSON) into tArray
put tArray["rate"] into field "rate"
this should work to put the rate into a fld named "rate".....
Hi, thanks for the code.
It says that there's an error with the arrayFromJSON part but I'm not sure how to solve it
Re: JSON
Posted: Sat Jun 07, 2014 5:47 pm
by sefrojones
If you read the comments in the example, I think it will become clear.....
hint: arrayfromJSON is a function of the EasyJSON library....
Re: JSON
Posted: Sat Jun 07, 2014 7:21 pm
by sefrojones
I will add that since this is such a small amount of data returned, perhaps LiveCode's text capabilities are enough to accomplish this. This method doesn't require any json library or use of arrays.
Code: Select all
on mouseUp
put "https://bitpay.com/api/rates/GBP" into tMarketURL
get url tMarketURL
put it into tMarketJSON
set the itemdelimiter to colon
put item 4 of tMarketJSON into tMarketRate
replace "}" with empty in tMarketRate
put tMarketRate into fld "Rate"
end mouseup