epcim of DevOps
1/19/2017 - 10:52 AM

jq parser json tricks

jq parser json tricks



# salt state execution results: (use with/without test=true, -b2, etc..)
# apt install -y jq collordiff
  salt \* state.apply --out=json --static test=true -b15 |\
  jq -r 'to_entries | map({ key:.key, value: { total: [.value[]]|length ,
                                       succeed: [.value[]|select(.result == true)]|length,
                                       failed:  [.value[]|select(.result == false)]|length,
                                       changes: [select(.value[].changes|length > 0)]|length  }}) | from_entries ' |\
  tee trend_$(date "+%Y-%m-%d-%s").json
  ls trend*.json| sort |tail -n2 |xargs -n2 diff -y | colordiff


# This produces a list of paths in the input:
  jq -c 'path(..)|[.[]|tostring]|join("/")'
  
  
# Parese all .yml files for given pattern (any depth) 
  #!/bin/zsh
  PATTERN=$@
  PATTERN=${PATTERN:-.parameters}
  for i in $(ls classes/**/*.yml); do echo $i; yaml2json $i | jq -ec "..|${PATTERN}| select(.!=null)" || printf '\e[A\e[K'; done