Need some examples for new JSON library as of LC8
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Need some examples for new JSON library as of LC8
Hello,
does anyone have some examples on how to use the new JSON library?
I'm strugling a bit with it.
I'm trying the example from this page:http://forums.livecode.com/viewtopic.ph ... ON#p155118
Using the json data which i load fine as a test file from my website.
Then i use the example on the last post from Jim on that page.
But it seems not to work.
So if anyone has some more basics to play with, it would be great.
thanks a lot.
does anyone have some examples on how to use the new JSON library?
I'm strugling a bit with it.
I'm trying the example from this page:http://forums.livecode.com/viewtopic.ph ... ON#p155118
Using the json data which i load fine as a test file from my website.
Then i use the example on the last post from Jim on that page.
But it seems not to work.
So if anyone has some more basics to play with, it would be great.
thanks a lot.
-
- VIP Livecode Opensource Backer
- Posts: 7228
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Need some examples for new JSON library as of LC8
How does it fail for you? One thing that does matter is that the JSON has to be well-formed or the function will fail. I usually wrap it in a "try" structure and report the error if one occurs. You can check for malformed JSON here:
http://json.parser.online.fr/
Here's what I usually do:
If the error is something about being unable to read the JSON, it's likely malformed. I usually see a list of LC error codes when that happens.
Some people have had better results using JsonImport() instead. You might try that too.
http://json.parser.online.fr/
Here's what I usually do:
Code: Select all
put url tURL into tJSON
try
put JSONToArray(tJSON) into tJSONArray
catch tErr
answer tErr -- this should give some clue
end try
Some people have had better results using JsonImport() instead. You might try that too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Need some examples for new JSON library as of LC8
Thanks, this is a nice thing to try. I will do so tomorrow and report back. I had to watch the quarter final Womens Soccer Euro Championship. Yoohoo we won again, now from Sweden: NL-SE 2-0 and now going to watch Germany-Denmark, the winner of that will have to compete the Dutch in the Semi-Final
***edit*** the match Ger-DM is prosponed, because of to much rain......
So i have some time now to check.
Thanks for the link: it had indeed 2 faults, now they are corrected. (it's just the json from the forum page as noted above, to test how this works exactly)
I do not get any error, but the widget is also not filled with data, maybe i'm overlooking something.
script in button:
script in stack:
***edit*** the match Ger-DM is prosponed, because of to much rain......
So i have some time now to check.
Thanks for the link: it had indeed 2 faults, now they are corrected. (it's just the json from the forum page as noted above, to test how this works exactly)
I do not get any error, but the widget is also not filled with data, maybe i'm overlooking something.
script in button:
Code: Select all
local tProfile,tURL,tJSON,tArray
on mouseUp
put "https://mysite.nl/folder/json_test/test.json" into tURL
put url tURL into tJSON
answer tJSON
if the result begins with "error" then
answer"something is not good"
else
answer"ok"
try
put JasonToArray("CRITERIA") into tArray
catch tErr
answer tErr -- this should give some clue
end try
set the arrayData of widget "Tree View" to tArray
end if
end mouseUp
Code: Select all
local tArray,tJSON,count
function JasonToArray whatType
answer"function called"
repeat for each key x in tJSON["feed"]
add 1 to count
if tJSON["feed"] [x] ["type"] is whatType then put tJSON["feed"] [x] into tArray[count]
end repeat
return tArray
end JasonToArray
Re: Need some examples for new JSON library as of LC8
don't know if it's a false flag, but Microsoft Essentials gets nervous of playing on that JSON parser site, using the items in the right corner Samples-->All JS Types
-
- VIP Livecode Opensource Backer
- Posts: 7228
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Need some examples for new JSON library as of LC8
I mis-read what you are doing. There is a built-in function named "JSONtoArray" and that's what I use. I see now that you are using a custom function, "JasonToArray" which is something different. It apparently extracts lines of a particular type from a larger JSON string. Using a "try" structure here won't do much, since the function doesn't throw any errors if there's a problem. You can take it out.
It looks like the JasonToArray function is expecting a LC array, and you are passing a JSON string instead. You'll want to use the built-in JSONtoArray() function to convert the string you receive from the URL into a LC array, and then pass that to the JasonToArray function. If you pass only the raw JSON string, there are no "keys" per se, it's just a string, so the repeat loop won't run and it will return empty.
Secondly, the JSON string is stored in script locals in both the button and in the stack, but that won't share the data between different scripts. A script local variable only has value for the handlers in the same script. You could use a global variable instead but it would be even better (and cleaner) if you pass the JSON as a parameter to the jasonToArray function. As it is now, tJSON will probably be empty in the stack script so there is nothing to process.
Finally, unless the variable "count" is used elsewhere in the stack script, there's no reason to have it as a script-local variable. It can be a handler variable instead.
It looks like the JasonToArray function is expecting a LC array, and you are passing a JSON string instead. You'll want to use the built-in JSONtoArray() function to convert the string you receive from the URL into a LC array, and then pass that to the JasonToArray function. If you pass only the raw JSON string, there are no "keys" per se, it's just a string, so the repeat loop won't run and it will return empty.
Secondly, the JSON string is stored in script locals in both the button and in the stack, but that won't share the data between different scripts. A script local variable only has value for the handlers in the same script. You could use a global variable instead but it would be even better (and cleaner) if you pass the JSON as a parameter to the jasonToArray function. As it is now, tJSON will probably be empty in the stack script so there is nothing to process.
Finally, unless the variable "count" is used elsewhere in the stack script, there's no reason to have it as a script-local variable. It can be a handler variable instead.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Need some examples for new JSON library as of LC8
Yes, thank you Jacque.
What was i thinking....i changed it somehow thinking it was a random name for the function, even though i did read it before in the dictionary.
Working now now i can test some more.
by the way, i googled some and this is also a nice json checkr without giving me security warnings https://jsonlint.com/
Thanks a lot for your clear advise!
What was i thinking....i changed it somehow thinking it was a random name for the function, even though i did read it before in the dictionary.
Working now now i can test some more.
by the way, i googled some and this is also a nice json checkr without giving me security warnings https://jsonlint.com/
Thanks a lot for your clear advise!
Re: Need some examples for new JSON library as of LC8
Please note of this bug: http://quality.livecode.com/show_bug.cgi?id=19698
so jsonimpot/jsonexport are not perfect.
this:
using jsonexport(jsonimport(temp))
becomes:
that is a totally different json file.
At the present there is no way to solve this bug, due the nature of the livecode arrays.
so jsonimpot/jsonexport are not perfect.
this:
Code: Select all
{
"Likes":[
"noise",
"animals",
"chocolate",
"hitting Izzy",
"running and screaming"
]
}
becomes:
Code: Select all
{
"Likes": {
"5": "running and screaming",
"1": "noise",
"2": "animals",
"3": "chocolate",
"4": "hitting Izzy"
}
}
At the present there is no way to solve this bug, due the nature of the livecode arrays.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Need some examples for new JSON library as of LC8
Yes i noted.
I'm struggling too, to see what's best for me.
As my thoughts are after importing and/or decoding it, it has to be treated as an array internally
so when is use this site http://www.convertcsv.com/csv-to-json.htm
it gives 4 choices of json files, just like you say MaxV
and after importing i'm trying to get it in a datagrid or table field for now to see how i can get a certain result when finding a key(or a value in a column on a certain line)
importing a database seems easier now to me then using this json, but maybe because i have to find the workings and understanding a lot more.
I'm struggling too, to see what's best for me.
As my thoughts are after importing and/or decoding it, it has to be treated as an array internally
so when is use this site http://www.convertcsv.com/csv-to-json.htm
it gives 4 choices of json files, just like you say MaxV
and after importing i'm trying to get it in a datagrid or table field for now to see how i can get a certain result when finding a key(or a value in a column on a certain line)
importing a database seems easier now to me then using this json, but maybe because i have to find the workings and understanding a lot more.
Re: Need some examples for new JSON library as of LC8
the post of Richard Gaskin here viewtopic.php?f=49&t=27980#p146704 got me on the right track
so i exported an ods (open file format) excel like sheet with 12 colums and 23 rows to csv file in LibreOffice.
Then i used JSONedit(you can get it here http://tomeko.net/software/JSONedit/) to get a similair output as the 1st of 4 options from the above mentioned converter-site, so simple csv2json. exported it to json.
then count the number of elements,because csv2json creates an array of objects with keys/values in it
then using combine and a repeat loop i can now find the element which contains the correct key i want to find and put it in a table field
will put an example here tomorrow. This way i did not have to change anything manually to the json file.
So nice learning curve here with this i must say. Did take a while....
so i exported an ods (open file format) excel like sheet with 12 colums and 23 rows to csv file in LibreOffice.
Then i used JSONedit(you can get it here http://tomeko.net/software/JSONedit/) to get a similair output as the 1st of 4 options from the above mentioned converter-site, so simple csv2json. exported it to json.
then count the number of elements,because csv2json creates an array of objects with keys/values in it
then using combine and a repeat loop i can now find the element which contains the correct key i want to find and put it in a table field
will put an example here tomorrow. This way i did not have to change anything manually to the json file.
So nice learning curve here with this i must say. Did take a while....
Re: Need some examples for new JSON library as of LC8
So the JSON file i have is similair like this, only only shortened it and changed the actual data:
Put below script in a button, use an entry field for the data to search for, use a table field to display the found data and search on US UK or EU
it will put both the key and the value of the found element into 2 columns of the datafield
Code: Select all
[
{
"Board" : "50",
"Counterweight" : "100,
"ElectricalPower" : "120",
"ElectricalType" : "US",
},
{
"Board" : "60",
"Counterweight" : "110",
"ElectricalPower" : "230",
"ElectricalType" : "UK",
},
{
"Board" : "70",
"Counterweight" : "120",
"ElectricalPower" : "230",
"ElectricalType" : "EU",
}
]
Code: Select all
local tMatrix,tArray,tSubArray,tSomuch,tThis,count
on mouseUp
put empty into field "Table Field"
put URL "https://mysite.nl/folder/json_test/test.json" into tMatrix
--answer tMatrix
if the result begins with "error" then
answer "File not loaded!"
exit to top
--else
--answer "File loaded OK!"
end if
put fld"search" into tThis
put JSONToArray(tMatrix) into tArray
put the number of elements of tArray into tSomuch--this counts the number of {"data"} between the [] brackets, in this example 3
repeat with x= 1 to tSomuch
repeat for each key y in tArray[tSomuch]
add 1 to count
if tArray[x][y] is tThis then put tArray[x] into tSubArray
end repeat
if the mouse is down then exit repeat--just for test to bail out
end repeat
combine tSubArray by return and tab
put tSubArray after field "Table Field"
end mouseUp