Working with JSON

Moderators: LCNeil, heatherlaine, kevinmiller, elanorb

Post Reply
elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 251
Joined: Fri Feb 24, 2006 9:45 am

Working with JSON

Post by elanorb » Thu Mar 20, 2014 11:52 am

Hi everyone

In yesterdays webinar someone asked about working with JSON objects, this will be covered later in the course in the Web Services lesson but for anyone who is interested there are a couple of libraries available from members of the LiveCode Community for working with JSON.

The first is LibJSON by Mark Smith, a library that converts back and forth between LiveCode arrays and json objects.

http://www.webring.org/l/rd?ring=runtim ... evstuff%2F

The other is mergJSON by Monte Goulding, a JSON encoding/decoding external supporting all the platforms LiveCode supports.

http://mergext.com/home/mergjson/

Kind regards

Elanor
Elanor Buchanan
Software Developer
LiveCode

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Working with JSON

Post by Bobisback » Thu Mar 27, 2014 10:13 pm

Hey,

Thanks for this. For some reason I cannot get LibjSON to work right. When I run it it just comes back with error in handler. I am not sure if I have it installed it right. Do I jsut put it into the plugins folder where mobgui is at? Do I need to put a #include or import at the top or anywhere in the project?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Working with JSON

Post by Simon » Thu Mar 27, 2014 11:03 pm

Hi Bobisback,
Normally you'd copy all the stack scripts to your own project, calling each function as needed.
You could attach it as a sub-stack I guess but that is just messy.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Working with JSON

Post by Bobisback » Thu Mar 27, 2014 11:09 pm

Ya that does not sound to great..... Just drag and drop or?

Also are all the function calls just global to my stack at that point or is there some other needed operation to get it to work right?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Working with JSON

Post by Simon » Thu Mar 27, 2014 11:23 pm

Hi again Sean,
Copy 'n Paste from libJson stack script to your own stack script.

All global, check out the button scripts, they are full of extra code for examples sake you're just interested in
arrayToJson(tArray)
json.format(arrayToJson(tArray),5)
and such.

Watch out the button jason.toArray has a custom property in it (as shown by "set the json of btn "json.toArray" to tJson")

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 251
Joined: Fri Feb 24, 2006 9:45 am

Re: Working with JSON

Post by elanorb » Fri Mar 28, 2014 11:44 am

Hi

The best way to use libJSON is to make the stack a substack of your application. You do this by opening the Property Inspector for the libJSON stack and setting its Main stack to your stack.

To make the commands and functions provided by the libJSON library available to the main stack we need to start using the substack. The start using command places a stack's script into the message path after the current stack and before any objects in the backScripts.

Code: Select all

on preOpenStack
   ## Insert the scripts implemented in the JSON library
   ## into the message path. Making them available.
   start using "libJson"
end preOpenStack
We will be covering this in more detail when we get to Lesson 6 but I hope that helps for now.

Kind regards

Elanor
Elanor Buchanan
Software Developer
LiveCode

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Working with JSON

Post by Bobisback » Mon Mar 31, 2014 10:43 pm

Sweet thanks for the reply.

I did add it in but when I try to use the command it gives me an error "card "contacts": execution error at line 43 (Function: error in function handler) near "json.toArray", char 11" I just do not know enough about live code to find the problem and fix it. I do not know if it is a runtime error, a syntax error or if I am passing it stuff wrong to the function. Is there any debug tools in livecode that I am missing?

Also on a side note I will just tell you what I am trying to do and maybe you can just help me. The basic problem is this, because I am not used to working with live code I cannot think of how to deal with data structures from an abstract point of view. Let me explain, normally when I pull something out of the database it is in JSON, I would then parse this into a data structure of an array of objects that hold this data. So lets say it is places, I would parse it into an array of place objects and each object then has the associated data like address, name, phone number, maybe Geo-loc, and other assorted data. So normally in any other coding language I would create a new class called a Place that I would then have fields or properties for these values. And then I would create an array that held this objects and then made a quick function that would iterate through this JSON string putting the values where they need to be in the objects. This to me would be really simple to do, but for the life of me I cannot figure out what needs to fall into place to get this to work with livecode.

So this is what I can gather. Cards are basically your versions of class's in the since they have there own properties, they have there own functions, and some other similarities with class's. So I guess I would start with making a "place" card that has the properties that I need, but then the question is how do I display these things. How can I map the card properties to the field screen. Also how would I take the array of what libJSON produces and get the data I need out of it (That is if I can get it to work). I do not know, now it seems like this does not work at all. I am to "stuck" in the old way of coding that this just does not make since to me.

Sorry if I sound mad I do not mean to be I am just frustrated at not being able to figure this out.

Thanks,
Sean

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Working with JSON

Post by LCNeil » Tue Apr 01, 2014 11:50 am

Hi Sean,

The function error that you are receiving will most likely mean that there is an error in the syntax of the function you are trying to call. Although, without seeing your scripts it is hard to figure out what this could be.

In regards to debugging, there is a great comment by Dave Kilroy on the following post which also links to a lesson on how to deb in LiveCode-

http://forums.runrev.com/viewtopic.php?f=73&t=19831

As Elanor has mentioned, we will be covering how to parse JSON data in LiveCode during Lesson 6. LibJson operates by taking a jsonObject and then converting this into a LiveCode Array. This data can then be manipulated in any way that you like (e.g. placed into fields, placed into a datagrid)

If you are looking at how classes can be emulated in LiveCode, then there is some discussion of this on the following page-

http://stackoverflow.com/questions/1679 ... from-other

The suggestion here and from yourself would be to use properties to store specific information. A really simple example of how to create a custom property and retrieve data from this property is shown below-

1) Create a new stack with a single card called “Test”

2) Add 2 buttons to the stack. Once called “Set” and one called “Get”

3) Add a field to your card and call it “Data”

4) Add the following script to the “Set” button-

Code: Select all

on mouseUp
   set the cTestCustomProperty of this card to "Some Test Data"
end mouseUp
5) Add the following script to the “Get” button

Code: Select all

on mouseUp
   put the cTestCustomProperty of this card into field "data"
end mouseUp
You should notice that the text from the custom property is placed in the data field.

I hope the above is of some assistance

Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
LiveCode – Realize fast, compile-free coding
-- 

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Working with JSON

Post by Bobisback » Tue Apr 01, 2014 4:00 pm

Sweet thanks for the explanation. It sounds great. I will get back with you it I have any questions!


Thanks,
Sean

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4570
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Working with JSON

Post by jacque » Tue Apr 01, 2014 7:31 pm

Just a data point: the original version of libJSON had some errors that were fixed later on. I wonder if you obtained an older version? I have been using the revised version without errors.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

altschuler.jon
Posts: 6
Joined: Tue Apr 08, 2014 9:12 pm

Re: Working with JSON

Post by altschuler.jon » Tue Apr 08, 2014 9:24 pm

** UPDATE **
I have figure out the issue:
The instructions included in the libJson stack are using incorrect syntax.
They say "put json.toArray(tJson) into tArray" when it should be "put jsonToArray(tJson) into tArray".

Hope this helps anyone else receiving the execution error.

Jon


I'm also getting the execution error that Bob was getting when using libJson.
I have copied the stack script from the libJson stack into my main stack and the variable I'm giving to the jsonToArray function seems to be a well-formed bit of JSON.

I'm using the Community version of LiveCode, could this be causing a problem?

Thanks,

Jon

Post Reply

Return to “idea2app and Coding School”