Escaping double Quote in JSON format- powershell , azure cli [duplicate] - json

This question already has answers here:
Error parsing parameter '--expression-attribute-values': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
(3 answers)
pwsh -Command is removing quotation marks
(3 answers)
Closed 2 years ago.
Please I am trying to pass a html string as value to the below powershell function "set-content" from azure cli but I don't think I am escaping the double quotes correctly. The value parameter only accepts strings, somehow, it does not get executed at the target machine with the double quote. I get the below error. please help!
az vm extension set --name CustomScriptExtension --vm-name MyWindowsVM -g TestsandRG --publisher Microsoft.Compute --settings '{"commandToExecute": "powershell set-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value \"<html><body><h2>Welcome to Azure! My name is $($env:computername) .<\/h2><\/body><\/html>\""}'

Related

adding a value with jq fails if the value has spaces [duplicate]

This question already has answers here:
Passing variables (containing spaces) to curl --data field
(3 answers)
Closed 17 days ago.
When I try to add a bash string, that contains spaces with jq an error message will be generated. The error doesn't occur, if there are no spaces.
the code that generates the error
value="value with spaces"
echo {} | jq ". +={"key":"'$value'"}"
error message
jq: error: syntax error, unexpected $end, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Unix shell quoting issues?) at <top-level>, line 1: . +={"key":"value jq: 1 compile error
expected output
{"key":"value with spaces"}
note
without spaces in the value bash variable, the command will run just fine
You need to properly format the string you are using in jq.
You can create the variable in the same way:
VAL="value with spaces"
And using it like this:
jq -n --arg myvar "${VAL}" '{key: $myvar}'
{
"key": "value with spaces"
}

Shell Script jq reading a json key with period (.) inside a variable [duplicate]

This question already has answers here:
Passing bash variable to jq
(10 answers)
"Invalid numeric literal" error from jq trying to modify JSON with variable
(1 answer)
Closed 4 years ago.
Here is the example json
{
"app": "K8s",
"version": "1.8",
"date": "2018-10-10"
}
In order to get the value of app, I can do this in jq as
jq '.app'
But what I want is, I want to pass the key to jq as a bash variable, i.e
bash_var="app"
jq '."${bash_var}"'
I'm getting the output as null instead of the value. What is the correct syntax to achieve this?
First, you need to port the bash variable into jq's context usign the --arg flag and access it inside the [..]
jq --arg keyvar "$bash_var" '.[$keyvar]' json

Why bash does not json split array with jq -r bash IFS explicitly defined variable [duplicate]

This question already has answers here:
Convert a JSON array to a bash array of strings
(4 answers)
Closed 2 years ago.
I am reading an array from a file
file.json
{
"content": ["string with spaces", "another string with spaces", "yet another string with spaces"]
}
#!/bin/bash
GROUP_ID_TEMP=( $(IFS=','; jq -r '.content' file.json) )
why does jq read content print or echo content as space separated array for the below codeblock at the whitespace rather than the comma ',' as explicitly stated?
for each in "${GROUP_ID_TEMP[#]}"
do
echo "$each" >> "file.txt"
done
Here's an easier way to reproduce your problem:
var=( $(IFS=','; echo "foo,bar" ) )
declare -p var
What you expect is declare -a var=([0]="foo" [1]="bar") but what you get is declare -a var=([0]="foo,bar")
This happens because IFS affects word splitting and not program output. Since there is no word splitting in the scope of your variable, it doesn't affect anything.
If you instead define it in the scope that does word splitting, i.e. the scope in which the $(..) expansion happens:
IFS=','
var=( $(echo "foo,bar") )
then you get the result you expect.

How can jq reference a property with a non-identifier character in the name (like "#")? [duplicate]

This question already has answers here:
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
Closed 3 years ago.
It seems unlikely this hasn't been asked, but I can not find it anywhere.
I am trying to filter response from elasticsearch and need to reference the #timestamp field. However, jq uses # to specify formatters and I find nothing in documentation about how to escape the # character.
$ jq '.hits.hits[] | ._source.#timestamp' < debug.response.json
jq: error: syntax error, unexpected $end, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.hits.hits[] | ._source.#timestamp
jq: 1 compile error
You can use the generic object index syntax, like:
.hits.hits[]._source["#timestamp"]

How to parse json file, where keys start with '--' characters (dash) [duplicate]

This question already has answers here:
jq not working on tag name with dashes and numbers
(2 answers)
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
Closed 3 years ago.
I have below JSON file and would like to update the values of these keys --resType and --LogLevel using jq.
{
"--resType": "FILE",
"--LogLevel": "INFO"
}
To do that, I am using below command.
./jq .--resType=Test config.json>test.json
But, I see an error that says
"jq: error: Test/0 is not defined at , line 1:".
"jq: 2 compile errors"