I am having trouble parsing the JSON in livecode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
dexxterr
Posts: 2
Joined: Wed Dec 01, 2021 1:35 pm

I am having trouble parsing the JSON in livecode

Post by dexxterr » Wed Dec 01, 2021 1:57 pm

I'm trying to create a simple mobile app that queries an API and parses the response to display certain values.

The mobile has 2 fields viz:
  • Button to query the api
  • Large text box to display the contents
In my livecode stack, I've the following inclusions:
  • JSON Library
  • mergJSON
  • tsNet
The api response is as follows:

Code: Select all

{
  "data": [
    {
      "id": 1,
      "date_created": "2021-11-08T17:12:03Z",
      "date_updated": "2021-11-22T16:08:55Z",
      "first_name": "Matt",
      "last_name": "Damon",
      "email": "mattdamon[at]livecode[dot]com",
      "phone": "9876543210",
      "dob": "1980-01-01",
      "password": "xxxxxxxxx",
      "plan_start": "2021-11-22T16:07:46Z",
      "plan_expiry": "2021-12-21T16:06:25Z"
    }
  ]
}
I want to parse the JSON to display the email field value in the textbox.

In my livecode stack:
  • The button is named as "getdata"
  • The textbox is named as "flddata"

In the button script, when I use the following code, the entire api response above gets displayed in the text box

Code: Select all

   put "<api url endpoint>" into tUrl
   put "Authorization: Bearer xxxxxxxxx" into tHeaders
   put tsNetGetSync(tUrl, tHeaders, tRecvHeaders, tResult, tBytes) into tData
   put tData into field "flddata"

But when I add the following code In the button script, I don't see the value of the email key.

Code: Select all

   put "<api url endpoint>" into tUrl
   put "Authorization: Bearer xxxxxxxxx" into tHeaders
   put tsNetGetSync(tUrl, tHeaders, tRecvHeaders, tResult, tBytes) into tData
   put JSONToArray(tData) into tDataArray
   put tDataArray["email"] into field "flddata"

The above doesn't work. Nothing happens. How do I parse the value of the key "email" from the json response and display it in the text box? For the life of me, I can't figure out what's wrong. Any help would be appreciated. Thanks a ton!

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I am having trouble parsing the JSON in livecode

Post by Klaus » Wed Dec 01, 2021 2:11 pm

Hi dexxterr,

welcome to the forum!

No idea why but you will find the email here:

Code: Select all

...
put JSONToArray(tData) into tDataArray
put tDataArray[tData][1]["email"] into fld "flddata"
...
Best

Klaus

P.S.
Personal note:
A little hello or something in the very first posting would not have hurt.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: I am having trouble parsing the JSON in livecode

Post by dunbarx » Wed Dec 01, 2021 3:17 pm

Dexter did not introduce himself, but he was very polite and friendly towards the end. :wink:

As for your problem, I do not ever deal with such things, so I may be talking through my hat.

I don't wear hats.

I see that you are good up to the point that you have data returned from the API. That seems to me to be the hard part. But the data is formatted with characters that are uniform in structure. Again, never doing this sort of thing, why cannot you simply use LiveCode directly to process that data?

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4000
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: I am having trouble parsing the JSON in livecode

Post by bn » Wed Dec 01, 2021 9:38 pm

Klaus wrote:
Wed Dec 01, 2021 2:11 pm

Code: Select all

...
put JSONToArray(tData) into tDataArray
put tDataArray[tData][1]["email"] into fld "flddata"
...
Should that not be

Code: Select all

put tDataArray["data"][1]["email"] into field "flddata"
note ["data"] instead of [tData]

Putting a breakpoint after the line: put JSONToArray(tData) into tDataArray
lets you inspect the structure of tDataArray

Kind regards
Bernd

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I am having trouble parsing the JSON in livecode

Post by Klaus » Wed Dec 01, 2021 9:46 pm

Hi Bernd,

yes, sorry, if course it has to read:

Code: Select all

...
put JSONToArray(tData) into tDataArray
put tDataArray["data"][1]["email"] into fld "flddata"
...
Best

Klaus

dexxterr
Posts: 2
Joined: Wed Dec 01, 2021 1:35 pm

Re: I am having trouble parsing the JSON in livecode

Post by dexxterr » Thu Dec 02, 2021 3:50 pm

Hello everyone,

Thank you for the prompt response and apologies for the abrupt request for help. I think I forgot my manners when I posted the request for help :oops:

I'm a newbie learning Livecode to create my own mobile and desktop apps. I have a couple of ideas that I want to implement but I figured I'll start with something small and basic and immediately ran into a roadblock which is now fixed.

So thank you all once again! Looking forward to learning more!

Cheers,
Dexxterr

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: I am having trouble parsing the JSON in livecode

Post by dunbarx » Thu Dec 02, 2021 5:57 pm

Dexxter.

Just for my information, and maybe to let you know that there are often many ways to do things in LC, did you consider that you could have taken the returned data and started right in with LC's own capabilities? I only mention this because it seems very possible, and would be a terrific learning experience in its own right.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”