stackyyflow stackyyflow - 2 months ago 8
Git Question

Git statistics API JSON

I want to get the number of commits per day per hour. With the following command, I am able to get the output in json format. However, I would like to know if I can add the key to the values in json format using command line?

curl https://api.github.com/repos/test/myrepo/stats/punch_card


Current output:

[
0,
2,
32
]


Expected output:

[
day: 0,
hour: 2,
commits: 32
]

Answer

Since you haven't specified anything beyond "command line", I'm assuming you want a bash-based solution. This simple (though kind of ugly) script will do what you want, while maintaining indentation (apart from the closing square bracket of the overall response):

#!/bin/bash

resp=$(curl https://api.github.com/repos/test/myrepo/stats/punch_card)

nextPref=""
for val in $resp
do
    echo "$nextPref $val"
    if [[ $val == "[" && $nextPref == "" ]]
    then
        nextPref="      "
    elif [[ $val == "[" && $nextPref == "   " ]]
    then
        nextPref="              day:"
    elif [[ $nextPref == "          day:" ]]
    then
        nextPref="              hour:"
    elif [[ $nextPref == "          hour:" ]]
    then
        nextPref="              commits:"
    elif [[ $nextPref == "          commits:" ]]
    then
        nextPref="      "
    fi
done