I have a key/value below in a JSON
JAVA_OPTS="JAVA_OPTS": "-Xms1g -Xmx2g -Dapi.version=v1 -Dspring.profiles.active=test -Dservice.name=xyz"
Using jq i want to insert one more string to this key/value pair "-Dhttps.protocols=TLSv1.2"
So the output should looks like
JAVA_OPTS="JAVA_OPTS": "-Xms1g -Xmx2g -Dapi.version=v1 -Dspring.profiles.active=test -Dservice.name=xyz -Dhttps.protocols=TLSv1.2"
You might be looking for something like that :
jq '.JAVA_OPTS |= . + " -Dhttps.protocols=TLSv1.2"'
Try it here.
Related
I would like to parse the following object
{
"test_account": {
"us_east_1": {
"i-a023adfa2": "Key=a,Value=c key=2,Value=3",
"i-23adfw34r": "Key=t,Value=n"
}
}
}
I pass account and region as arguments to the script and am able to get the object but not able to parse the returned object. I want to extract key which is ec2-instance id and value which is combination of tags that needs to be added. I am using shell script to parse this json using Jquery.
Here is partial script..
instances = ${jq -r ".$1 | select .${2//[-]/_} != null) | .${2//[-]/_}". <path of json file> | tr '\n' ' ')
I call this with "sh <scriptname> test-account us-west-2" ...
I couldn't parse the returned object to get key and value into respective variables. Can someone please help ?
Thanks.
I am looking to filter a JSON stream based on its keys. Here is the public JSON file:
https://s3.amazonaws.com/okta-ip-ranges/ip_ranges.json that I am trying to wrangle. When I filter this for keys jq 'keys', I get the following output
[
"apac_cell_1",
"emea_cell_1",
"emea_cell_2",
"preview_cell_1",
"preview_cell_2",
"preview_cell_3",
"us_cell_1",
"us_cell_10",
"us_cell_11",
"us_cell_12",
"us_cell_2",
"us_cell_3",
"us_cell_4",
"us_cell_5",
"us_cell_6",
"us_cell_7"
]
I am trying to get all the ip_ranges associated with the keys starting with "us_cell_*" and I have not found a way to do it. Most of the filtering seems to be focused on the values and not the keys.
You can use the following :
to_entries | map(select(.key | startswith("us_cell_")) | .value.ip_ranges) | add
Try it here.
to_entries maps the root object into an array of objects with key and value fields corresponding to the fields of the original object.
We filter that to retain only those which have a key starting with "us_cell_", map it further to keep only the ip ranges and finally merge those arrays together.
I currently have output from jq that looks like this:
1§
{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}
2§
{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}
and I would like it join the output lines per record, i.e.:
1§{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}
2§{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}
Here is the sample input and current filter: https://jqplay.org/s/qBfGyriA5B
If I use -j then I get everything on the same line, which is not what I want
1§{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}2§{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}
One way would be to use the join function by empty string on the created objects. Note that the object you are creating needs to converted to string type for it to work. Use the filter below with the --raw-output/-r mode
.stations.station[] + { lastUpdate: .stations."#lastUpdate" } |
[ .id + "§", tostring ] |
join("")
jqplay demo
I have the following json data:
{"jsonrpc":"2.0","result":[],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"2392","hostid":"10953","macro":"{$GATEWAY}","value":"10.25.230.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"1893","hostid":"12093","macro":"{$GATEWAY}","value":"10.38.118.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"2400","hostid":"14471","macro":"{$GATEWAY}","value":"10.25.230.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"799","hostid":"10798","macro":"{$GATEWAY}","value":"10.36.136.1"}],"id":1}
{"jsonrpc":"2.0","result":[],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"1433","hostid":"10857","macro":"{$GATEWAY}","value":"10.38.24.129"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"842","hostid":"13159","macro":"{$GATEWAY}","value":"10.38.113.1"}],"id":1}
{"jsonrpc":"2.0","result":[],"id":1}
I am trying to extract the value of the "value" field from each line. jq -r '.result[].value' <jsonfile> works perfectly but it does not take into account the JSON lines where there is no "value" field. I would like it to print an empty line for them. Is this possible with jq?
You can use this:
jq -r '.result[].value // "" ' a.json
This uses the or operator //. If .result[].value is present, the value will get printed, otherwise an empty line gets printed.
This would work:
jq -r '.result | if length > 0 then .[0].value else "" end'
Since false // X and null // X produce X, .result[].value // "" may not be what you want in all cases.
To achieve the stated goal as I understand it, you could use the following filter:
.result[] | if has("value") then .value else "" end
I want to grep a certain amount of string from a TCL variable and use that in my tool command. Example:
${tcl_Var} - this contains string like VEG_0_1/ABC
I want to grep from above string until it the point it hits first forward slash, so in the above case it would be VEG_0_1. And then replace it in my command. Example:
VEG_0_1/REST_OF_THE_COMMAND.
Don't think in terms of grep, think about "string manipulation" instead.
Use regsub for "search and replace:
% set tcl_Var VEG_0_1/ABC
VEG_0_1/ABC
% set newvar [regsub {/.+} $tcl_Var {/REST_OF_THE_COMMAND}]
VEG_0_1/REST_OF_THE_COMMAND
Alternately, your problem can be solved by splitting the string on /, taking the first component, then appending the "rest of the command":
% set newvar "[lindex [split $tcl_Var /] 0]/REST_OF_THE_COMMAND"
VEG_0_1/REST_OF_THE_COMMAND
Or using string indices:
% set newvar "[string range $tcl_Var 0 [string first / $tcl_Var]]REST_OF_THE_COMMAND"
VEG_0_1/REST_OF_THE_COMMAND
You can do this with regular expressions using the regsub TCL command. There is no need to run the external program grep. See more info here: http://www.tcl.tk/man/tcl8.4/TclCmd/regsub.htm
If you are new to regular expressions, read TCL-specific tutorial about them.
set tcl_Var VEG_0_1/ABC
set varlist [split $tcl_Var "/"]
set newvar [lindex $varlist 0]/REST_OF_THE_COMMAND