jq: replache slash by dash chracter - jq

I need to replace slash character by "-".
I mean:
[
"1934/08/20",
"1961/01/10",
"1952/01/25",
"1967/07/24"
]
I need:
[
"1934-0820",
"1961-01-10",
"1952-01-25",
"1967-07-24"
]
Is there anyway to get it?

Since you want to replace all the / with -'s, gsub is the way to go:
jq 'map(gsub("\/"; "-"))'
Will produce
[
"1934-08-20",
"1961-01-10",
"1952-01-25",
"1967-07-24"
]
As you can test in this online demo

Use strptime to parse the dates, then strftime to format them. map can map that filter over your entire list.
$ cat tmp.json
[
"1934/08/20",
"1961/01/10",
"1952/01/25",
"1967/07/24"
]
$ jq 'map(strptime("%Y/%m/%d") | strftime("%F"))' tmp.json
[
"1934-08-20",
"1961-01-10",
"1952-01-25",
"1967-07-24"
]
(%F is a shortcut, where available, for %Y-%m-%d.)

Related

JQ error Cannot index string with number with scan

I have error with scan function, why?
https://jqplay.org/s/E-0qbbzRPS
I need do this without -r
There are two issues with your filter. Firstly, you need to separate parameters to a function with semicolon ;, not comma ,:
scan("([0-9A-Za-z_]+) == '([0-9A-Za-z_]+)"; "g")
Secondly, scan with two parameters is not implemented (in contradiction to the manual).
jq: error: scan/2 is not defined at <top-level>, line 1:
But as you are using scan, your regex will match multiple occurrences anyway, so you may as well just drop it :
.spec.selector | [scan("([0-9A-Za-z_]+) == '([0-9A-Za-z_]+)") | {(.[0]): .[1]}]
[
{
"app": "nginx"
}
]
Demo

flattening json to csv with jqplay

I'm trying to flatten a json file to .csv. I'd like to use jqplay for this in stead of programming it in python for example.
The example below is een array that als contains arrays.
My desired output is one line entry on the 2nd array:
so
OPEN, NR1, ....
CLOSED, NR2, ...
....
Can anyone help me with a good jq command for this?
[
{
"description": "Berendrechtsluis",
"lock_id": "BES",
"longitude_wgs84": 4.28561,
"latitude_wgs84": 51.34414,
"lock_doors": [
{
"state": "OPEN",
"lock_door_id": "NR1",
"operational_state": "NO_DATA",
"state_since_in_utc": "2021-12-29T16:32:23Z",
"longitude_wgs84": 4.28214,
"latitude_wgs84": 51.34426
},
{
"state": "CLOSED",
"lock_door_id": "NR2",
"operational_state": "WORKING",
"state_since_in_utc": "2022-01-12T12:32:52Z",
"operational_state_since_in_utc": "2021-12-22T13:13:57Z",
"longitude_wgs84": 4.28247,
"latitude_wgs84": 51.34424
},
....
Are you looking for something like this?
jq -r '.[].lock_doors[] | [.[]] | #csv'
"OPEN","NR1","NO_DATA","2021-12-29T16:32:23Z",4.28214,51.34426
"CLOSED","NR2","WORKING","2022-01-12T12:32:52Z","2021-12-22T13:13:57Z",4.28247,51.34424
Demo
To add column headers, simply prepend them in an array:
jq -r '["a","b","c"], .[].lock_doors[] | [.[]] | #csv'
"a","b","c"
"OPEN","NR1","NO_DATA","2021-12-29T16:32:23Z",4.28214,51.34426
"CLOSED","NR2","WORKING","2022-01-12T12:32:52Z","2021-12-22T13:13:57Z",4.28247,51.34424
Demo

Select version string from JSON array and increment it by one using jq

Bash script find a a tags in ECR repo:
aws ecr describe-images --repository-name laplacelab-backend-repo
\ --query 'sort_by(imageDetails,& imagePushedAt)[*]'
\--output json | jq -r '.[].imageTags'
Output:
[
"v1",
"sometag",
...
]
How I can extract the version number? v<number> can contain the only version tag. I need to get a number and increment version for the set to var. If output of sort_by(imageDetails,& imagePushedAt)[*] is empty JSON arr instead
[
{
"registryId": "057296704062",
"repositoryName": "laplacelab-backend-repo",
"imageDigest": "sha256:c14685cf0be7bf7ab1b42f529ca13fe2e9ce00030427d8122928bf2d46063bb7",
"imageTags": [
"v1"
],
"imageSizeInBytes": 351676915,
"imagePushedAt": 1593514683.0
}
]
Set 2
No one repo sort_by(imageDetails,& imagePushedAt)[*] return [] set 1.
As a result, I try to get var VERSION with next version for an update or 1 if the repo is empty.
You could use the select() function on the imageTags array and get only the tag starting with v and increment it.
jq '( .[].imageTags[] | select(startswith("v")) | ltrimstr("v") | tonumber | .+1 ) // 1'
For other cases like the tags array being empty or containing null strings (error case), the value defaults to 1
For storing into the variable e.g. say version (avoid using uppercase variable names from a user scripts), use command substitution. See How do I set a variable to the output of a command in Bash?
version=$( <your-pipeline> )
Note: This does not work well with version strings following Semantic versioning RFC, e.g. as v1.2.1 as jq does not have a library to parse them.

jq syntax help for querying lists output

I need help in correcting jq test cases syntax. Following is output file & trying to test ID list with command below. Gives error index to string type.
[[ $(echo $output| jq -r '.output.value[] | select(.identity).id_list') == *"id2"* ]]
output = {
"resource_output": {
"value": {
"identity": [
{
"id_list": [
"/subscriptions/---/id1",
"/subscriptions/---/id2",
"/subscriptions/--/id3"
],
"principal_id": "",
"tenant_id": "",
"type": "managed"
}
]
}
}
Your query does not match the sample JSON, and you have not indicated what output you are expecting, but the following variation of your query illustrates how to use select and test with your data along the lines suggested by your attempt:
echo "$output" |
jq -r '.resource_output.identity[].id_list[] | select(test("id2"))'
Output:
/subscriptions/---/id2

How to do parse json array dynamically in shell script using jq too in shell script

Suppose I have the following json in a file json.txt
{
"first_name": "John",
"last_name": "Smith",
"things_carried": [
"apples",
"hat",
"harmonica"
],
"children": [
{
"first_name": "Bobby Sue",
"last_name": "Smith"
},
{
"first_name": "John Jr",
"last_name": "Smith"
}
]
}
In shell script I had written the logic to find the size of children array using jq tool .
size=cat json.txt | jq '.children | length'
i=0
while [ $i -le $size ]
do
array[$i]=$(cat json.txt | jq '.children[$i]')
i=`expr $i + 1`
done
On running this it gives the following error -
.children[$i] 1 compile error
It seems that it is not able to replace the variable i in the children[] array , as because if we give the expression -
array[$i]=$(cat json.txt | jq '.children[0]')
it runs well .
Can someone help me .
You're using single quotes around the jq program. Shells do not interpolate variables inside single quotes; this is intentional and the jq manual recommends using single quotes around programs for this reason.
An argument syntax is provided by jq for this purpose. This syntax allows you to set jq variables to the value of shell variables. You could replace your current jq invocation with this:
array[$i]=$(cat json.txt | jq --arg i $i '.children[$i | tonumber]')
It looks like you're just trying to set the children to a bash array variable.
You don't need to loop, just set the array directly.
$ IFS=$'\n'; array=($(jq -c '.children[]' json.txt))
You should use the following syntax:
array[$i]=$(cat json.txt | jq '.children['${i}']')

Resources