How to escape backticks in string literal [duplicate] - go

This question already has answers here:
How to escape back ticks
(7 answers)
Closed 3 years ago.
I have a slack payload that would format to markdown. And I'm trying to figure out how to preserve the backtick
var jsonStr = []byte(`{
"channel": "#edtest",
"username": "snapshot",
"attachments": [
{
"mkdwn": true,
"text": "`this backtick doesn't work`",
}
]
}`)
if you look at the text field, that backtick won't work

You can't escape backticks. When there's long text like this, one thing you can do is to replace them:
var jsonstr=[]byte(strings.Replace(`{
Some json string with ^backticks^
}`,"^","`",-1))
Another option is to add string segments:
var jsonstr=[]byte(`{
Some json string with `+"`backticks`"+`
}`)

Related

Removing Special Characters from JSON in NIFI

I have JSON input for Nifi flow with some special characters. Could someone help me with how to remove special characters following payload? we would need only value with array and double-quotes.
input json:
{
"TOT_NET_AMT": "["55.00"]",
"H_OBJECT": "File",
"H_GROSS_AMNT": "["55.00"]",
"TOT_TAX_AMT": "[9.55]"
}
Expected Result :
{
"TOT_NET_AMT": "55.00",
"H_OBJECT": "File",
"H_GROSS_AMNT": "55.00",
"TOT_TAX_AMT": "9.55"
}

Issue with creating a byte object [duplicate]

This question already has answers here:
how to put a backquote in a backquoted string?
(3 answers)
Closed 2 years ago.
I am trying to initialize a JSON object as a byte in go lang. Here, I am attaching two exmples
var countryRegionData = []byte(`{"name": "srinivas"}`)
var countryRegionData = []byte(`{"name": "srini`vas"}`)
In the first initilization there is no issue, all working as expected.
In the second initialization if you see there is ` between i and v. I have some requirement like this. How to achieve?
A backtick cannot appear in a raw string literal. You can write something like this:
var countryRegionData = []byte("{\"name\": \"srini`vas\"}")
You cannot use escaping in a raw string literal. Either you have to use double-quoted string:
"{\"name\": \"srini'vas\"}"
Or do something like:
`{"name": "srini`+"`"+"vas"}`

Elasticsearch escape hyphenated field in groovy script

I am attempting to add a field to a document doing something similar to https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates. However, I appear to be running into issues due to the field being hyphen separated(appears to be treated as a minus sign) as opposed to underscore separated.
Example body below:
{"script":"ctx._source.path.to.hyphen-separated-field = \"new data\""}
I attempted to escape the hyphens with a backslash, but to no luck.
You can access the field using square brackets, i.e. simply do it like this:
{"script": "ctx._source.path.to['hyphen-separated-field'] = \"new data\""}
This one worked for me on 2.x (or maybe other version as well):
"script": {
"inline": "ctx._source.path.to[field] = val",
"params": {
"val": "This is the new value",
"field": "hyphen-separated-field"
}
}
Or this will also work
{"script": "ctx._source.path.to.'hyphen-separated-field' = 'new data'"}

Convert string to hashes and output in json format in ruby

I have a string object which is returned from the controller like below.
details = "{"name"=>"David", "age"=>"12", "emp_id"=>"E009", "exp"=>"10",
"company"=>"Starlink"}"
So the details.class would be String.
I need to convert it as Hash and output in Json format.So the output would be in below
format. I know that using eval method it can be done. But I think there will be security issues for it. So please suggest the best way to do it.
{
"name":"David",
"age":"12",
"emp_id":"E009",
"exp":"10",
"company":"Starlink"
}
How do I achieve it. Please help
It looks like you should go to your API vendor, and tell him he has a bug, since Hash.inspect is not a valid serialization, as it is not standard, and may not always be reversible.
If what you get is in the form above though, you can treat it as a JSON after running gsub on it:
formatted_details = JSON.pretty_generate(JSON.parse(details.gsub('=>', ':')))
puts formatted_details
# => {
"name": "David",
"age": "12",
"emp_id": "E009",
"exp": "10",
"company": "Starlink"
}

Replace text in a Visual Studio Code Snippet Literal

Is it possible to replace text in a Visual Studio Snippet literal after the enter key is pressed and the snippet exits its edit mode?
For example, given a snippet like this:
public void $name$
{
$end$
}
If I were to enter $name$ as:
My function name
is it possible to have Visual Studio change it to:
My_function_name
or
MyFunctionName
After many years there is an answer, for anyone still coming across this question:
"Replace Whitespaces": {
"prefix": "wrap2",
"body": [
"${TM_SELECTED_TEXT/[' ']/_/gi}",
],
"description": "Replace all whitespaces of highlighted Text with underscores"
},
Add this to your user snippets. Or alternativly you can add a keyboard shortcut like this:
{
"key": "ctrl+shift+y",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "${TM_SELECTED_TEXT/[' ']/_/gi}"
}
},
Hope this helps anyone coming across this in the future
It's great. I used it to wrap things in a function with quotes. If a selection has quotes it can remove the quotes. In the snippet part, it seems to break down into:
TM_SELECTED_TEXT - this is the input
[ ' '] - regex find
_ - regex replace
gi - global flag for regex
So what I wanted is change: "User logged in" into: <%= gettext("User logged in") %>
For this in the snippet I used:
"body": ["<%= gettext(\"${TM_SELECTED_TEXT/['\"']//gi}\") %>"],
Note: you need to escape the quotein the regular expression, therefore: " becomes \".
In my case the goal was to replace a single instance of the word Authoring with Baker:
${TM_FILENAME_BASE/(Authoring)/Baker/}

Resources