Accessing JSON output

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Accessing JSON output

Post by aetaylorBUSBnWt » Thu Jul 23, 2020 12:58 am

Hello,

I am new to LiveCode, long history of writing just about everything else, so my thought processes are more class oriented and \" is how you put a double quote into a string, been that way for 50 years, where did \q come from?

So it took me a while to find a way to input a JSON sample string into LiveCode.
Reading a text file works best - don't have to fart around with quote games.
local myText;
local theFilePath;
answer file "A text file"
if it <> "" then
put it into theFilePath
put URL ("file:" & theFilePath) into myText
end if

Now that I have a sample JSON string stuck into LiveCode, I did the following:

local tArray;
put JsonImport(myText) into tArray;

OK, now I have some unknown structure in tArray.
In the debugger I can see:
Screenshot 2020-07-22 19.51.59.png
Now how do I access the pieces of whatever it is that is?

Thanks,
Andrew

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Accessing JSON output

Post by FourthWorld » Thu Jul 23, 2020 4:52 am

The User Guide included with LiveCode (see the Help menu) has a very good discussion on learning to use arrays, beginning on page 120.

An an example, to get the value of the accountID key of the first account:

Code: Select all

get tArray["AccountListResponse"]["Accounts"]["Account"][1][accountId]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Accessing JSON output

Post by jacque » Thu Jul 23, 2020 5:33 pm

where did \q come from?
Good question, it isn't LC. I think you've found a custom function that many people use to avoid excessive concatenation in scripts. The one-line function just returns a given string with quotes around it. It improves readability.

The \" syntax is used in LC when constructing several things, like any command or function that uses regex expressions (merge comes to mind, as well as matchtext, etc.) Even menu items use a backslash to force an exact character match if the character is one that indicates menu text styles.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mrcoollion
Posts: 719
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Accessing JSON output

Post by mrcoollion » Fri Jul 24, 2020 8:18 am

Also take a look at Andy's brand new post on a JSON plugin for this purpose (JSON Api Helper plugin).
The code will also help to learn about JSON and Arrays.
viewtopic.php?f=4&t=34434&p=194462#p194462

Post Reply

Return to “Internet”