JSON problem...[SOLVED]

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 2636
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

JSON problem...[SOLVED]

Post by stam » Tue May 18, 2021 9:05 am

Hi all,

I've used mailJet's email API for sending users randomised password when they reset their password, and it's worked fine (also great because it's completely free up to a level of sent emails i'll never reach ;) )

However, I'm having some difficulty construction the JSON required from liveCode arrays. I thought arrayToJSON would do it, but it doesn't seem to create JSON arrays or sub-arrays.

In particular, the message structure required is as below (taken from MailJet's cURL API):

Code: Select all

{"Messages":[
	{
	   "From": {
			   "Email": "pilot@mailjet.com",
			   "Name": "Mailjet Pilot"
						},
	   "To": [
			   {
			   "Email": "passenger1@mailjet.com",
			   "Name": "passenger 1"
			   }
		   ],
	   "Subject": "Your email flight plan!",
	   "TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
	   "HTMLPart": "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
	}
   ]
}
I would have thought creating an array Messages[] and populating Messages[From][Email] for example, and then use arrayToJSON(Messages[]) would do it and superficially it does seem to.

However, very crucially, arrayToJSON doesn't seem to create JSON arrays (the output contains no square brackets, just curly braces that denote JSON objects rather than arrays) and i can't seem to see a way to force arrays, which in this case are required for the API.

I always presume it's likely something i've not done properly when things don't work, and happy to be told i'm doing something wrong! But in this case i wonder if it could be an issue with arrayToJSON?

I could just construct the message bit by bit, but i was hoping for a more elegant solution... any suggestions?
Last edited by stam on Tue May 18, 2021 9:52 am, edited 1 time in total.

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: JSON problem...

Post by elanorb » Tue May 18, 2021 9:30 am

Hi stam,

I think you need the "Messages" element inside the array you convert to JSON e.g.

Code: Select all

on mouseUp
   put "pilot@mailjet.com" into tArray["Messages"][1]["From"]["Email"]
   put "Mailjet Pilot" into tArray["Messages"][1]["From"]["Name"]
   put "passenger1@mailjet.com" into tArray["Messages"][1]["To"][1]["Email"]
   put "passenger 1" into tArray["Messages"][1]["To"][1]["Name"]
   
   put arrayToJSON(tArray,,true)
end mouseUp
Gives me

Code: Select all

{
  "Messages": [
    {
      "From": {
        "Email": "pilot@mailjet.com",
        "Name": "Mailjet Pilot"
      },
      "To": [
        {
          "Email": "passenger1@mailjet.com",
          "Name": "passenger 1"
        }
      ]
    }
  ]
}
Kind regards

Elanor
Elanor Buchanan
Software Developer
LiveCode

stam
Posts: 2636
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: JSON problem...

Post by stam » Tue May 18, 2021 9:51 am

elanorb wrote:
Tue May 18, 2021 9:30 am
I think you need the "Messages" element inside the array you convert to JSON
Fantastic, that did it.. thank you!!

Well, that and the fact that i hadn't numerically keyed the elements of the intended JSON array/sub-array :oops: :oops:

Thank you for the swift response!
Stam

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”