Quick question. I'm receiving as input a JSON value with double quotes escaped with a backslash, I need to output them in html entities. Is there a filter that can achieve this? Thanks!
Input
{ foo: "60\"x50\"" }
Output
60"x50"
I believe you need a "|s" filter. Given data from your example {foo|s} will output 60"x50"
Related
I want all numerical data to be formatted with 2 decimal places.
Is there a way to set this in the template word file (where I output the variable value via
<<[variableName] >>
), or even globally?
To format a numeric expression result, you can specify a format string as an element of the corresponding expression tag.
<<[variableName]:"0.##">>
See the following article for more information:
https://docs.aspose.com/words/net/outputting-expression-results/
I'm constructing a JSON string by concatenating strings. To get the quotes correct for the web service (no quotes around numbers), I'm using escape characters. When I print the resulting string in Xcode, it looks fine.
{"number":999,"name":"new"}
But when I use Wireshark to capture what's going over the wire, I can see the escape characters in the string.
"{\"number\":999,\"name\":\"new\"}"
Here's the code that creates the string:
let jsonString:String = "{\"number\":" + num + ",\"name\":\"" + name + "\"}"
How can I create the string so the escape characters aren't there?
Thanks
The reason I couldn't send the JSON as a dictionary is that Swift dictionaries are unordered. In this case, the server is using MongoDB. I fixed the issue server side instead of trying to hack around it in the client.
Here's the reason: "Why does it happen: MongoDB uses a binary data format called BSON. In BSON, the order of keys always matters. Notice, in JSON an object is an unordered set of key/value pairs."
http://devblog.me/wtf-mongo
I'm fairly certain that the escape characters are being inserted by Wireshark in it's own output.
I have a text input for a search field where the string is then passed to an EntityQuery. When ever the query includes a single quote I get a message like the following:
There is an unterminated string literal at position 39 in 'substringof(O'Malley,FirstName) eq true'.
It even happens when just hard coding the query like this:
var query = breeze.EntityQuery
.from("Users")
.expand("GroupUsers.Group")
.where("lastName", "contains","O'Malley")
.skip(skipAmt)
.take(pageSize)
.inlineCount(true);
I've tried escaping the single quote by doing double single quotes or doing \' and it still comes back with an error. This also happens similarly with double quotes. What is the proper way to escape the string literal characters?
I can't repro this. You should be able to escape a single ' by simply doubling it. For example, the following query works without a problem on v 1.2.8.
var q = EntityQuery.from("Employees")
.where("lastName", "contains", "O''Malley");
Does the problem still occur if you 'simplify' the query down to just the where 'clause'?
I seen this code on a website
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
The result of this gave some really long code in nos,digits, and slashes and couldn't figure it out. Is it like a security trick or something like that.
I just cant seem to understand what is this "replace" function trying to achieve ?? If anyone could explain what does it mean...
These are regular expressions (called regex for short).
The actual expression is inside /.../ with \ beeing an escape character.
So /^\// holds an ^/ regex.
As for your exact expressions:
1. ^/: the / character at the begging of line (^)
2. (index|default).[a-zA-Z]{3,4}$:
3. the /$: / character at the end of line ($)
I have a JSON object and I want to convert it escaping / characters.
Object:
{
"id":"123",
"name":"test",
"link":"https://google.com"
}
Desired result:
{
"id":"123",
"name":"test",
"link":"https:\/\/google.com"
}
How can I do this transformation in Ruby, RoR?
If it is at all possible, modify the values before they are JSON'd. In activerecord, I believe you can change the value and convert it to JSON - so long as you don't save the model, that change will be discarded.
In ruby, JSON is just a string, so you could do
my_json.gsub('/', '\\/')
This would convert any forward slashes in the keys, too. I don't know of any reason a JSON string would contain forward slashes outside of a string, so that should be fine.
If you want to avoid converting the keys, you could use a (slightly complicated) regular expression:
my_json.gsub(/:\s*"[^"]*\/[^"]*"/) { |m| m.gsub('/', '\\/') }
This finds a section that starts with a colon, possibly some whitespace after that, then some double quotes. It then looks for some optional stuff (anything that isn't a double quote), then a forward slash, them more stuff that isn't a double quote, then an actual double quote. So essentially, the minimum it will find is :"/" - it then passes each matching string into the block, and runs the previous gsub to convert the slashes. The output of the block then replaces whatever was found in the initial gsub.
I'm sure there are neater ways, so play around.