Page 1 of 1

ArrayToJSON(tdata)

Posted: Wed Nov 04, 2020 3:02 pm
by mimu
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?

Re: ArrayToJSON(tdata)

Posted: Tue Mar 09, 2021 7:39 pm
by andresdt
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