0
{
    "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 to get both state variables in an array i.e. running and standby. So far i have tried

curl -s http://localhost:9099/api | grep state | sed 's/"//g' | awk -F  ": " '/state/ {print $2}' | tr '\n' ' ' |  sed s'/..$//'
2
  • dont have package jq or python installed in the server. Commented Jul 28, 2017 at 3:14
  • You can edit the detail in the question no need to put that in comment and also your code is not indented properly please indent that properly Commented Jul 28, 2017 at 3:29

2 Answers 2

2

FYI you don't need a pipeline of 20 different commands when you're using awk. The command line you provided can be written as just one command:

awk -F'"' '$2=="state"{printf "%s%s", (++c>1?", ":""),$4}'

but all you really need is:

awk -F'"' '$2=="state"{print $4}'

and to save the output in a shell array (assuming your json is really always formatted as you show in your question) would be:

$ arr=( $(cat file | awk -F'"' '$2=="state"{print $4}') )
$ echo "${arr[0]}"
Running
$ echo "${arr[1]}"
Standby

Replace cat file with your curl command.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use jq

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

Or if you want all the entries no matter how many

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

Alternatively, if you don't have jq (or just looking for ways of feeling pain) you can parse most json in bash using ticktick -- which is written in 250 lines of bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.