ArrayToJSON(tdata)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mimu
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 76
Joined: Tue Mar 05, 2013 7:00 pm
Location: Berlin
Contact:

ArrayToJSON(tdata)

Post by mimu » Wed Nov 04, 2020 3:02 pm

I have an livecode array which has to be converted to json, see attachments.

If i use:

Code: Select all

put ArrayToJSON(tdata,"string") into tdata
then everything in json is of type string (first Attachment)

If i use

Code: Select all

put ArrayToJSON(tdata) into tdata 
then numbers are of type Int32 or Double (second Attachment),

This json is almost as i need it,
but the key mProdNo should always be of type string.

What would be the best way to achieve it?
Attachments
Bildschirmfoto 2020-11-04 um 14.38.37.png
Bildschirmfoto 2020-11-04 um 14.43.33.png

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

Re: ArrayToJSON(tdata)

Post by andresdt » Tue Mar 09, 2021 7:39 pm

if you put a char not numeric, in front of the value of the key mProdNo. Converting it to JSON will always convert it to a string.
example:

Code: Select all

repeat for each key k in tData["xParts"]
	if tData["xParts"][k]["mProdNo"] is not empty and tData["xParts"][k]["mProdNo"] begins with ":" then
	put ":" before tData["xParts"][k]["mProdNo"] 
	end if
end repeat
It is not a very nice solution :oops: :oops: , but it is a solution.

Now you must keep in mind that when reading the data you must remove this character.

Code: Select all

repeat for each key k in tData["xParts"]
	if tData["xParts"][k]["mProdNo"] begins with ":" then
		delete char 1 of tData["xParts"][k]["mProdNo"] 
	end if
end repeat

Post Reply

Return to “Talking LiveCode”