I need to make a curl POST method with a JSON:
request=`$home/post.curl $url " { \"name\": \"aaaaaaa\", \"message\": \"I NEED TO NOT INTERPRET THIS VAR \${rich_message}\" "`
But the server who reads the request gets "Message=I NEED TO NOT INTERPRET THIS VAR ".
Who can I do please?
If you need the entire JSON string to be passed through literally, put it in single quotes:
request=$($home/post.curl "$url" '{ "name": "aaaaaaa",
"message": "I NEED TO NOT INTERPRET THIS VAR \${rich_message}"}$)
(Notice that I added the missing closing } and changed the obsolescent backticks to the preferred $(...) syntax. I also split the string across multiple lines for legibility. Finally, I put "$url" in double quotes, as this type of string should usually be properly quoted.)
If you require the entire string to be in double quotes, the syntax you were using would require an additional backslash (backticks work similarly to double quotes; you need another escape to escape the escape, as it were); but switching to $(...) fixes that as a side effect, too.
The \ infant of $ is wrong.
Also you are missing } at the end in order to have proper JSON.
So with proper JSON should be:
" { \"name\": \"aaaaaaa\", \"message\": \"I NEED TO NOT INTERPRET THIS VAR ${rich_message}\" }"
Related
I'm seeing a is not defined at <top-level> when calling jq like so:
jq ".Changes[0].ResourceRecordSet.Name = word-is-here.domain.com" someFile.json
The error repeats for each word separated by a dash in the second side of the replacement. The full error is like
jq: error: word/0 is not defined at <top-level>, line 1:
.Changes[0].ResourceRecordSet.Name = word-is-here.domain.com
I've tried escaping quotes in many different ways but that didn't help. (what I mean by this is doing "'"'" weird stuff, I'm still learning bash so I'm just trowing stuff at the wall until it sticks)
EDIT:
So I'm trying to run this in a bash script, and both side of the = signs are variables such as jq --arg value "$value" --arg key "$key" '$key = $value' "$path" (what I tried after a suggestion)
and got the error:
Invalid path expression with result ".Changes[0].ResourceRecor...
The json I'm using is as such:
{
"Changes": [
{
"Action": "do something",
"ResourceRecordSet": {
"Name": "some name here to replace",
...
}
}
]
}
jq '.Changes[0].ResourceRecordSet.Name = "word-is-here.domain.com"' file.json
Quote the string you are assigning. Or pass it to jq via an argument:
jq --arg foo 'words-here' '.Changes[0].ResourceRecordSet.Name = $foo' file.json
For passing the path to the key you want as an argument, a suggestion from https://github.com/stedolan/jq/issues/1493 might work:
jq --argjson path '["Changes",0,"ResourceRecordSet","Name"]' \
--arg val 'word-is-here.domain.com' \
'getpath($path) = $val' file.json
The problem (or at least the obvious problem) here is evidently the string: word-is-here.domain.com, since jq is interpreting the dash ("-") as an operation ("minus").
Unfortunately, since you haven't given us many clues, it's not completely clear what specifically needs to be changed, but a reasonable guess is that word-is-here.domain.com is intended as a fixed string. If so, you would have to present it as a JSON string. So in a bash or bash-like environment, you could write:
jq '.Changes[0].ResourceRecordSet.Name = "word-is-here.domain.com"' someFile.json
Specifying the LHS path via a shell variable
If the LHS path must be specified by a shell variable, it should if possible be passed in as a JSON array, e.g. using the --argjson command-line option; one can then use an expression of the form setpath($path; $value) to update the path.
If for some reason a solution allowing the LHS to be specified as a jq path is preferred, then shell string-interpolation could be used, though as with any such interpolation, this should be done with care.
I want to replace '\\n' with '\n'
i am using the gsub method but cannot replace
ruby
{
code => "#mystring=event.get('stockLines');
#mystring=#mystring.gsub('\\\n', '\n');"
}
You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.
Read more
In your case:
#mystring.gsub("\\n", "\n")
The essential difference between the two literal forms of strings (single or double quotes) is that double quotes allow for escape sequences while single quotes do not!
Escaping of the backslash can be a bit weird.
For a regular Ruby script the code would be:
#mystring = event.get('stockLines').gsub('\\\\\n', '\n')
It seems like in logstash config the code is also in a string, so it needs to be escaped once more, i.e.:
ruby
{
code => "#mystring = event.get('stockLines').gsub('\\\\\\\\\\n', '\\n');"
}
I have this in my YAML file:
test: I want spaces before this text
In my case I would like to have a space before the text in my array or json when converted. Is that possible? How?
With JSON as output it's parsed like this:
{
"test": "I want spaces before this text"
}
No spaces.
You can test it here
You would have to quote your scalar with either single or double quotes instead of using a plain scalar (i.e. one without quotes). Which one of those two is more easy to use depends on whether there are special characters in your text.
If you use single quotes:
test: ' I want spaces before this text'
this would require doubling any single quotes already existing in your text (something like ' abc''def ').
If you use double quotes:
test: " I want spaces before this text"
this would require backslash escaping any double quotes already existing in your text (something like " abc\"def ").
With \t this work
Example:
var options = {
\t hostname: 'localhost',
\t port: 4433
};
Someone please help to explain how this work? About the single quote it should not interpret anything but it is not working as what i expected. I expect to get echo $testvar value exactly '"123b"'.
a="testvar"
b="'"123b"'"
eval $a='$b'
echo $testvar
'123b'
a="testvar"
b='"123b"'
eval $a='$b'
echo $testvar
"123b"
a="testvar"
b='"123b"'
eval $a=$b
echo $testvar
123b
Guessing that you want testvar to be <single-quote><double-quote>123b<double-quote><single-quote>:
testvar=\'\"123b\"\'
Consider this in C or Java:
char* str = "123b";
printf("%s\n", str);
String str = "123b";
System.out.println(str);
Why does this write 123b when we clearly used double quotes? Why doesn't it write "123b", with quotes?
The answer is that the quotes are not part of the data. The quotes are used by the programming language to determine where strings start and stop, but they're not in any way part of the string. This is just as true for Bash as for C and Java.
Just like there's no way in Java to differentiate Strings created with "123" + "b" and "123b", there's no way in Bash to tell that b='"123b"' used single quotes in its definition, as opposed to e.g. b=\"123b\".
If given a variable you want to assign its value surrounded by single quotes, you can use e.g.
printf -v testvar "'%s'" "$b"
But this just adds new literal single quotes around a string. It doesn't and cannot care how b was originally quoted, because that information is stored.
To instead add a layer of escaping to a variable, so that when evaluated once it turns into a literal string identical to your input, you can use:
printf -v testvar "%q" "$b"
This will produce a value which is quoted equivalently but not necessarily identically to your original definition. For "value" (a literal with double quotes in it), it may produce \"value\" or '"value"' or '"'value'"' which all evaluate exactly to "value".
I want to get the quoted string in the line below into a string exactly as-is, but I am tripping on the necessary escape sequences for Ruby. mycommand.cmd is really a wrapper for powershell.exe so I want to preserve everything between the quotes, and hold onto the escape characters that are already there.
mycommand.cmd "^|foreach-object { \"{0}=={1}\" -f $_.Name, $_.Found }"
Use single quotes :
ruby-1.9.3-p0 :001 > '^|foreach-object { \"{0}=={1}\" -f $.Name, $.Found }'
=> "^|foreach-object { \\\"{0}=={1}\\\" -f $.Name, $.Found }"
The only escape characters in single quotes are : \' and \\