0

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?

4
  • do you mean the output will be : Lines, 16.937233 ? Commented Feb 18, 2014 at 2:46
  • Ideally I would want just 16.937233 Commented Feb 18, 2014 at 2:53
  • Your json file is not valid at line 9. Commented Feb 18, 2014 at 2:54
  • use python for such tasks... Commented Feb 18, 2014 at 8:02

1 Answer 1

2

Please use the wonderful json util jq:

jq -r '.results.elements[] | select(.name=="Lines").ratio' input.json
Sign up to request clarification or add additional context in comments.

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.