linux - Parse json into array in bash -


{     "db_status": {         "sysa": {             "taskname": "ab",             "state": "running",             "status": "system attention",             "updated": "0727",             "version": "5"         },          "sysb": {             "taskname": "null",             "state": "standby",             "status": "system ok",             "updated": "0727",             "version": "6"         }     } } 

curl command returns json object. trying both state variables in array i.e. running , standby. far have tried

curl -s http://localhost:9099/api | grep state | sed 's/"//g' | awk -f  ": " '/state/ {print $2}' | tr '\n' ' ' |  sed s'/..$//' 

you use jq

$ curl -s http://localhost:9099/api | jq '.db_status.sysa.state, .db_status.sysb.state' "running" "standby" 

or if want entries no matter how many

$ curl -s http://localhost:9099/api | jq '.db_status[].state' "running" "standby" 

alternatively, if don't have jq (or looking ways of feeling pain) can parse json in bash using ticktick -- written in 250 lines of bash


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -