Skip to content

how-to-use-jq

reference

sample

snapshot sort

select

t = pyjq.all('.entities[] | select (.uuid==\"' + i + '\") ', full)

output

jq -r '[...]|@csv' |column -t -s','
jq -r '[...]|@tsv' |column -t 
# add table head
az resource list |jq -r '\
["name","resgrp"], ["--","--"], \
(.[] | [.name, .resourceGroup])|@tsv' |column -t

az resource list |jq -r '\
(["name", "resgrp", "Owner"] | (., map(length*"-"))), \
(.[] | [.name, .resourceGroup, .tags.owner//"-"])|@tsv' |column -t

if else

aws translate list-text-translation-jobs |jq -r '.TextTranslationJobPropertiesList[] | (if .JobStatus == "IN_PROGRESS" then .JobStatus, .JobName, .JobId else empty end)' |xargs

combine output to a array

... |jq -s .

filter value based on regexp

#get all script from blueprint
cat *.json | jq -r '.. | select (.|tostring|test("^#!.*"))'

and
#sample, replace REGEXHERE to your string
#Key name is "Name", replace it
| select((.Tags[]|select(.Key=="Name")|.Value) | match("REGEXHERE") )

edit-json-file-directly-

jq '. += { "cpuManagerPolicy":"static"}' /etc/kubernetes/kubelet/kubelet-config.json

good sample

developer:
  android:
    members:

    - alice
    - bob
    oncall:
    - bob
hr:
  members:
  - charlie
  - doug
this:
  is:
    really:
      deep:
        nesting:
          members:
          - example deep nesting

to

developer-android-members:

  - alice
  - bob
developer-android-oncall:
  - bob
hr-members:
  - charlie
  - doug
this-is-really-deep-nesting-members:
  - example deep nesting

code

yq . | # convert yaml to json using python-yq
    jq ' 
    . as $input | # Save the input for later
    . | paths | # Get the list of paths 
        select(.[-1] | tostring | test("^(members|oncall|priv)$"; "ix")) | # Only find paths which end with members, oncall, and priv
        . as $path | # save each path in the $path variable
    ( $input | getpath($path) ) as $members | # Get the value of each path from the original input
    {
        "key": ( $path | join("-") ), # The key is the join of all path keys
        "value": $members  # The value is the list of members
    }
    ' |
    jq -s 'from_entries' | # collect kv pairs into a full object using slurp
    yq --sort-keys -y . # Convert back to yaml using python-yq

reinvent breakout session

download json

youtube-dl https://www.youtube.com/playlist?list=PL2yQDdvlhXf-Jdg0SkHt85s-YvTUaNmgT --skip-download --write-info-json --write-annotations

get title url and description

cat *json |jq -r '[
(if (.title|test("\\([A-Z]{3}[0-9]{3}")) then (.title|scan("[A-Z]{3}[0-9]{3}")) else (" ") end),
.title,
.webpage_url,
(.description
|gsub("\n";"#")
|gsub("\"";"")
|gsub("#Subscribe:.*$";"")
|gsub("Learn more about[^#]*#";""))
]|@csv'  > ../a.txt

upload a.txt

match following line

xx xx xx xx xx xx xx (CON312 xx xx xx xx

and print following line, if not match, then print ” “

CON312

#!/bin/bash

for i in $a ; do
    file=$(ls |egrep '\('"$i"'\)' )
    if [[ -z $file ]]; then
        echo $i
    else
        url=$(cat "$file" |jq -r '.webpage_url')
        echo $i $url
    fi
done

get lengh

jq -r '.TransitGateways | length'

install jq

https://stedolan.github.io/jq/download/

install jq on CentOS7

sudo wget -O /usr/local/bin/jq 'https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64'

install pyjq on mac

brew remove jq
brew install ruby (ruby > 2.3)
brew install --HEAD jq
pip install pyjq

install pyjq on CentOS 7

yum groupinstall -y 'Development Tools'
yum install python-devel
pip install pyjq

yum install python2-pip
pip install —upgrade pip