Page 1 of 1

Accessing JSON output

Posted: Thu Jul 23, 2020 12:58 am
by aetaylorBUSBnWt
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

Re: Accessing JSON output

Posted: Thu Jul 23, 2020 4:52 am
by FourthWorld
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]

Re: Accessing JSON output

Posted: Thu Jul 23, 2020 5:33 pm
by jacque
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.

Re: Accessing JSON output

Posted: Fri Jul 24, 2020 8:18 am
by mrcoollion
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