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.
Escape Sequence?
Moderator: Martin
Re: Escape Sequence?
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:
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:
Regards,
Martin
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"})';
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"})'}
Martin
Re: Escape Sequence?
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.
I get a sense this is the wrong approach considering your release notes.
Thanks!
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.
I get a sense this is the wrong approach considering your release notes.
What's the recommended way to pass in JSON?1.31.0 (2016-07-16)
- added new jsonformat for inline scripts to format values to JSON fragments
Thanks!
Re: Escape Sequence?
Hi,
Your example is one of the ways that work. Personally I prefer to prepare such values in a Script first:
and in the HTTP action just write:
{data}
The jsonformat could also be used. Prepare the map in a Script first:
and in the HTTP action write:
{data,jsonformat}
Regards,
Martin
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}';
{data}
The jsonformat could also be used. Prepare the map in a Script first:
Code: Select all
data = newMapFromValues("on", false);
{data,jsonformat}
Regards,
Martin
Re: Escape Sequence?
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.
Is there a benefit to one over the other? I want to get in the habit of doing things the "right" way.
Re: Escape Sequence?
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
Regards,
Martin
Re: Escape Sequence?
Awesome. That makes perfect sense. I'll go with the newMapFromValues and jsonformat approach. It's clearly the most failproof.
Thanks again!
Thanks again!