Page 1 of 1
Need some examples for new JSON library as of LC8
Posted: Sat Jul 29, 2017 2:20 pm
by sphere
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.
Re: Need some examples for new JSON library as of LC8
Posted: Sat Jul 29, 2017 8:34 pm
by jacque
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:
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
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.
Re: Need some examples for new JSON library as of LC8
Posted: Sat Jul 29, 2017 8:43 pm
by sphere
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:
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
script in stack:
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
Posted: Sat Jul 29, 2017 9:16 pm
by sphere
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

- trojan.PNG (5.34 KiB) Viewed 9319 times
Re: Need some examples for new JSON library as of LC8
Posted: Sun Jul 30, 2017 5:38 am
by jacque
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.
Re: Need some examples for new JSON library as of LC8
Posted: Sun Jul 30, 2017 9:49 pm
by sphere
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!
Re: Need some examples for new JSON library as of LC8
Posted: Wed Aug 09, 2017 12:08 pm
by MaxV
Please note of this bug:
http://quality.livecode.com/show_bug.cgi?id=19698
so jsonimpot/jsonexport are not perfect.
this:
Code: Select all
{
"Likes":[
"noise",
"animals",
"chocolate",
"hitting Izzy",
"running and screaming"
]
}
using jsonexport(jsonimport(temp))
becomes:
Code: Select all
{
"Likes": {
"5": "running and screaming",
"1": "noise",
"2": "animals",
"3": "chocolate",
"4": "hitting Izzy"
}
}
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.
Re: Need some examples for new JSON library as of LC8
Posted: Wed Aug 09, 2017 1:24 pm
by sphere
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.
Re: Need some examples for new JSON library as of LC8
Posted: Thu Aug 10, 2017 10:31 pm
by sphere
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....

Re: Need some examples for new JSON library as of LC8
Posted: Fri Aug 11, 2017 4:37 pm
by sphere
So the JSON file i have is similair like this, only only shortened it and changed the actual data:
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",
}
]
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
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
it will put both the key and the value of the found element into 2 columns of the datafield