Posts

Showing posts with the label jq

jq: Select property value using regex

jq: Select property value using regex I have the following json Object: { "foo": { "name": "Name 1", "color": "green", "something_else": { "name" : "Name 2" } }, "bar": { "name": "Something else", "color": "red" } } To get all possible parents properties of the property called "name" using jq I tried : path(recurse|select(.name? !=""))[0] And it works and give back : "foo" "foo" "bar" Now I want to apply regex to filter the property value, say I want to consider only all properties called name that have a value beginning with "Name" and followed by a number like "Name 2" , to get: name "Name 2" "foo" "foo" I tried this: path(recurse|select(.name? =~ match(/Name */)))[0] How to use mat...

jq shell script: aggregate iteration content into json body

jq shell script: aggregate iteration content into json body I'm creating a shell script. I need to create a curl json body according the a key-value list content. This content is splitted by way of a awk which generate a two column table: key-value awk KVS_VARIABLES=$(awk -F= '!($1 && $2 && NF==2) { print "File failed validation on line " NR | "cat 1>&2"; next } { print $1, $2 }' $f) Example output: VAR1 VAL1 VAR2 VAL2 VAR3 VAL3 So, this table is iterated on a while iteration and each key and value are splitted: key value echo "$KVS_VARIABLES" | while read -r kv do key=$(echo $kv | awk '{print $1}') value=$(echo $kv | awk '{print $2}') done So, I need some way to aggregate this content into a json document in order to send it out using curl : curl curl -k -X PUT -d @- -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" -H "Content-Type: applica...

jq array of hashes to csv

jq array of hashes to csv So I have a data source like this: { "things": [ { "thing_name": "Bestco", "thing_id": 1 }, { "thing_name": "GreatCo", "thing_id": 2 }, { "thing_name": "DressCo", "thing_id": 3 } ] } I want to get output like this: $ echo '{"things":[{"thing_name":"Bestco","thing_id":1},{"thing_name":"GreatCo","thing_id":2},{"thing_name":"DressCo","thing_id":3}]}' | jq -r '.things | map(.thing_name, .thing_id, "n") | @csv' | sed -e 's/,"$//g' -e 's/^",//g' -e 's/^"$//g' "Bestco",1 "GreatCo",2 "DressCo",3 $ Using a fake parameter seems like a hack and then needs to be clean up by sed to work. How do I do this with just jq...

Sort / filter multiple objects in JQ by date

Sort / filter multiple objects in JQ by date I'm trying to use JQ to find the most recent artifact in a Nexus API query. Right now, my JSON output looks something like: { "items" : [ { "downloadUrl" : "https://nexus.ama.org/repository/Snapshots/org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1.0-20180703.144121-1.jar", "path" : "org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1.0-20180703.144121-1.jar", "id" : "V0FEQS1TbmFwc2hvdHM6MzhjZDQ3NTQwMTBkNGJhOTY1N2JiOTEyMTM1ZGRjZWQ", "repository" : "Snapshots", "format" : "maven2", "checksum" : { "sha1" : "7ac324905fb1ff15ef6020f256fcb5c9f54113ca", "md5" : "bb25c483a183001dfdc58c07a71a98ed" } }, { "downloadUrl" : "https://nexus.ama.org/repository/Snapshots/org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1...