Escape Sequence?

Post your questions and help other users.

Moderator: Martin

Post Reply
gockets
Posts: 1
Joined: 16 May 2014 01:01

Escape Sequence?

Post by gockets » 16 May 2014 01:04

Hi all,

I can't seem to readily find info about this on the website. I am using the HTTP Request action to POST data. The data that I need to post is:

JSON.stringify({password:password,sceneName:Test,activate:"1"})

However, after the left squiggly bracket the text turns aqua. I can't seem to find the escape sequence that I need to put before the special characters (or surround them with)?

Any help would be greatly appreciated! And I'm sorry in advance if this is well documented somewhere, but my searching fails me.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Escape Sequence?

Post by Martin » 16 May 2014 06:14

Hi,

Automagic tries to interpret the contents within curly braces as a script.
You could assign the script in an action Script to a variable first:

Code: Select all

data = 'JSON.stringify({password:password,sceneName:Test,activate:"1"})';
It's important to use single quotes so that Automagic does not try to replace variables within the string literal (see last section in topic String Inline Expressions of action Script).
You can then use {data} in the HTTP Request action.

You can also inline the script in the HTTP Request action directly without assigning to a variable first. The following text can be used in the data field:

Code: Select all

{'JSON.stringify({password:password,sceneName:Test,activate:"1"})'}
Regards,
Martin

User avatar
GollyJer
Posts: 15
Joined: 19 Sep 2016 01:01

Re: Escape Sequence?

Post by GollyJer » 08 Oct 2016 15:23

Hi Martin,
I was messing around yesterday trying to turn on/off my Hue lights and couldn't get JSON.stringify to work.

Eventually it worked by wrapping the string in curly braces and quotes.
Image

I get a sense this is the wrong approach considering your release notes.
1.31.0 (2016-07-16)
- added new jsonformat for inline scripts to format values to JSON fragments
What's the recommended way to pass in JSON?

Thanks!

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Escape Sequence?

Post by Martin » 10 Oct 2016 18:45

Hi,

Your example is one of the ways that work. Personally I prefer to prepare such values in a Script first:

Code: Select all

data = '{"ok": false}';
and in the HTTP action just write:
{data}

The jsonformat could also be used. Prepare the map in a Script first:

Code: Select all

data = newMapFromValues("on", false);
and in the HTTP action write:
{data,jsonformat}

Regards,
Martin

User avatar
GollyJer
Posts: 15
Joined: 19 Sep 2016 01:01

Re: Escape Sequence?

Post by GollyJer » 11 Oct 2016 01:13

Thanks Martin. Those all work great.
Is there a benefit to one over the other? I want to get in the habit of doing things the "right" way. :)

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Escape Sequence?

Post by Martin » 12 Oct 2016 18:37

All three options are OK. The value {'{"on": false}'} is perfectly fine but just looks a bit strange. I would probably use the Script with the string constant if you don't need any dynamic content in the JSON. As soon as the JSON is composed and contains some string values or user input, I would go for the newMap-Script variant and build the JSON-string dynamically. The jsonformat will ensure that the resulting JSON is properly escaped so when a user inputs something like That's "great" the JSON will still be correct.

Regards,
Martin

User avatar
GollyJer
Posts: 15
Joined: 19 Sep 2016 01:01

Re: Escape Sequence?

Post by GollyJer » 12 Oct 2016 19:14

Awesome. That makes perfect sense. I'll go with the newMapFromValues and jsonformat approach. It's clearly the most failproof.
Thanks again!

Post Reply