Pi Horse
Pi Horse

Reputation: 2430

Parsing json while executing a curl

I am doing the following curl request in my bash script to get a coverage report for my build :

codeCoverage=$(curl -s $url/job/$jobname/$lastBuildNo/cobertura/api/json?depth=2)

This returns a json reponse which looks like this:

{
"results": {
    "children": [
        {
            "children": [
                {}
            ],
            "elements": [
                {},
                {}
            ],
            "name": "com.test.web"
        },
        {
            "children": [
                {}
            ],
            "elements": [
                {},
                {}
            ],
            "name": "com.test.web.filters"
        }
    ],
    "elements": [
        {
            "name": "Classes",
            "ratio": 37.75
        },
        {
            "name": "Conditionals",
            "ratio": 11.63969
        },
        {
            "name": "Files",
            "ratio": 42.70073
        },
        {
            "name": "Lines",
            "ratio": 16.937233
        },
        {
            "name": "Methods",
            "ratio": 23.67906
        },
        {
            "name": "Packages",
            "ratio": 50
        }
    ],
    "name": "Cobertura Coverage Report"
}

}

I am just interested in the

"name": "Lines",
"ratio": 16.937233

I can parse the text once I get it and extract that value, but is there any way to just get that value when I make the curl request?

Upvotes: 0

Views: 247

Answers (1)

kev
kev

Reputation: 161604

Please use the wonderful json util jq:

jq -r '.results.elements[] | select(.name=="Lines").ratio' input.json

Upvotes: 2

Related Questions