Issue with parsing the JSON in livecode

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
isabellspivey
Posts: 1
Joined: Mon Aug 18, 2025 3:20 am

Issue with parsing the JSON in livecode

Post by isabellspivey » Mon Aug 18, 2025 3:28 am

Hello,
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:
1. Button to query the api
2. Large text box to display the contents

In my livecode stack, I've the following inclusions:
1. JSON Library
2. mergJSON
3. 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": "John",
      "last_name": "Doe",
      "email": "john . doe@unknown . 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:

1. The button is named as "getdata"
2. The textbox is named as "flddata"

In the button script, I've added the following code:

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"
But this doesn't work. Nothing happens. Any help would be appreciated. Thanks a ton!

stam
Posts: 3081
Joined: Sun Jun 04, 2006 9:39 pm

Re: Issue with parsing the JSON in livecode

Post by stam » Mon Aug 18, 2025 8:48 am

I would suggest setting a breakpoint and inspecting what is in your array. Then you'll see the structure of the array created:
Screenshot 2025-08-18 at 10.46.20.png
Instead of tDataArray["email"], you need to reference this as tDataArray["data"][1]["email"]

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 166
Joined: Tue Feb 23, 2010 10:53 pm

Re: Issue with parsing the JSON in livecode

Post by bobcole » Mon Aug 18, 2025 9:19 pm

isabellspivey:
Here is a simple way to get the data from a server.
However, it does not test the password or encrypt the data.

Code: Select all

on mouseUp
   put url("https://xxxxxx.com/QueryTest.json") into tData 
   put JSONToArray(tData) into tDataArray
   put tDataArray["data"][1]["email"] into field "fldData"
end mouseUp
I hope this helps.
Bob

stam
Posts: 3081
Joined: Sun Jun 04, 2006 9:39 pm

Re: Issue with parsing the JSON in livecode

Post by stam » Mon Aug 18, 2025 10:30 pm

bobcole wrote:
Mon Aug 18, 2025 9:19 pm
I hope this helps.
Bob
Bob, the code provided by the OP already takes into account the bearer ID/security by setting the headers, and arguably is more robust since it uses tsNet.

It's the JSON output/conversion to array that wasn't handled correctiy.

9 times out of 10, the problem is immediately fixable by inspecting what the data actually contains when trying to reference and getting unexpected results, hence my previous comment...

if the OP finds it too tedious to type the correct path to data (i.e. tDataArray["data"][1]["email"] instead of tDataArray["email"]), a simple modification to the code allows for directly accessing the subways:

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["data"][1] into tDataArray # <-- now the next line will work as expected.
   put tDataArray["email"] into field "flddata"
 

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 166
Joined: Tue Feb 23, 2010 10:53 pm

Re: Issue with parsing the JSON in livecode

Post by bobcole » Mon Aug 18, 2025 11:51 pm

Stam:
I forgot to give you credit for showing how to access the email variable. Good catch.
In case anyone is interested, I provide the full script as the OP wanted.
Also, I show the values of the tRecvHeaders, tResult and tBytes variables.
Bob

Code: Select all

on mouseUp
   put "https://xxxxxx.com/QueryTest.json" into tUrl
   put "Authorization: Bearer xxxxxxxxx" into tHeaders
   
   put tsNetGetSync(tUrl, tHeaders, tRecvHeaders, tResult, tBytes) into tData
   put JSONToArray(tData) into tDataArray
   put tDataArray["data"][1]["email"] into field "fldData"
   
   put tRecvHeaders into field "RecvHeaders"    
   put tResult into field "Result"
   put tBytes into field "Bytes"
end mouseUp

Post Reply