Output JSON from Bash script

后端 未结 6 1327
暖寄归人
暖寄归人 2020-12-07 18:00

So I have a bash script which outputs details on servers. The problem is that I need the output to be JSON. What is the best way to go about this?

相关标签:
6条回答
  • 2020-12-07 18:04

    I'm not a bash-ninja at all, but I wrote a solution, that works perfectly for me. So, I decided to share it with community.

    First of all, I created a bash script called json.sh

    arr=();
    
    while read x y; 
    do 
        arr=("${arr[@]}" $x $y)
    done
    
    vars=(${arr[@]})
    len=${#arr[@]}
    
    printf "{"
    for (( i=0; i<len; i+=2 ))
    do
        printf "\"${vars[i]}\": ${vars[i+1]}"
        if [ $i -lt $((len-2)) ] ; then
            printf ", "
        fi
    done
    printf "}"
    echo
    

    And now I can easily execute it:

    $ echo key1 1 key2 2 key3 3 | ./json.sh
    {"key1":1, "key2":2, "key3":3}
    
    0 讨论(0)
  • 2020-12-07 18:07

    I find it much more easy to create the json using cat:

    cat <<EOF > /your/path/myjson.json
    {"id" : "$my_id"}
    EOF
    
    0 讨论(0)
  • 2020-12-07 18:09
    data=$(echo  " BUILD_NUMBER : ${BUILD_NUMBER} , BUILD_ID : ${BUILD_ID} , JOB_NAME : ${JOB_NAME} " | sed 's/ /"/g')
    
    output => data="BUILD_NUMBER":"29","BUILD_ID":"29","JOB_NAME":"OSM_LOG_ANA"
    
    0 讨论(0)
  • 2020-12-07 18:14

    I wrote a tiny program in Go, json_encode. It works pretty good for such cases:

    $ ./getDistro.sh | json_encode
    ["my.dev","Ubuntu 17.10","4 days, 2 hours, 21 minutes, 17 seconds"]
    
    0 讨论(0)
  • 2020-12-07 18:21

    @Jimilian script was very helpful for me. I changed it a bit to send data to zabbix auto discovery

    arr=()
    
    while read x y;
    do
        arr=("${arr[@]}" $x $y)
    done
    
    vars=(${arr[@]})
    len=${#arr[@]}
    
    printf "{\n"
    printf "\t"data":[\n"
    
    for (( i=0; i<len; i+=2 ))
    do
         printf "\t{  "{#VAL1}":\"${vars[i]}\",\t"{#VAL2}":\"${vars[i+1]}\"  }"
    
        if [ $i -lt $((len-2)) ] ; then
            printf ",\n"
        fi
    done
    printf "\n"
    printf "\t]\n"
    printf "}\n"
    echo
    

    Output:

        $ echo "A 1 B 2 C 3 D 4 E 5" | ./testjson.sh
    {
        data:[
        {  {#VAL1}:"A", {#VAL2}:"1"  },
        {  {#VAL1}:"B", {#VAL2}:"2"  },
        {  {#VAL1}:"C", {#VAL2}:"3"  },
        {  {#VAL1}:"D", {#VAL2}:"4"  },
        {  {#VAL1}:"E", {#VAL2}:"5"  }
        ]
    }
    
    0 讨论(0)
  • 2020-12-07 18:26

    If you only need to output a small JSON, use printf:

    printf '{"hostname":"%s","distro":"%s","uptime":"%s"}\n' "$hostname" "$distro" "$uptime"
    

    Or if you need to produce a larger JSON, use a heredoc as explained by leandro-mora. If you use the here-doc solution, please be sure to upvote his answer:

    cat <<EOF > /your/path/myjson.json
    {"id" : "$my_id"}
    EOF
    

    Some of the more recent distros, have a file called: /etc/lsb-release or similar name (cat /etc/*release). Therefore, you could possibly do away with dependency your on Python:

    distro=$(awk -F= 'END { print $2 }' /etc/lsb-release)
    

    An aside, you should probably do away with using backticks. They're a bit old fashioned.

    0 讨论(0)
提交回复
热议问题