Page 1 of 1

I am having trouble parsing the JSON in livecode

Posted: Wed Dec 01, 2021 1:57 pm
by dexxterr
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!

Re: I am having trouble parsing the JSON in livecode

Posted: Wed Dec 01, 2021 2:11 pm
by Klaus
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.

Re: I am having trouble parsing the JSON in livecode

Posted: Wed Dec 01, 2021 3:17 pm
by dunbarx
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

Re: I am having trouble parsing the JSON in livecode

Posted: Wed Dec 01, 2021 9:38 pm
by bn
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

Re: I am having trouble parsing the JSON in livecode

Posted: Wed Dec 01, 2021 9:46 pm
by Klaus
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

Re: I am having trouble parsing the JSON in livecode

Posted: Thu Dec 02, 2021 3:50 pm
by dexxterr
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

Re: I am having trouble parsing the JSON in livecode

Posted: Thu Dec 02, 2021 5:57 pm
by dunbarx
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