How to convert shell output to JSON?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have this output in the following and I am trying to convert it to JSON api format. I want to know how can I do it.



 rock64@rockpro64:~$ sh MACscript.sh 
eth0
11:1d:11:11:11:1d
lo
00:00:00:00:00:00


Do I have to use python script or can I do it using shell script?



This is my MACshell script:



 rock64@rockpro64:~$ cat MACscript.sh 
!/bin/bash
getmacifup.sh: Print active NICs MAC addresses
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q unknown $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done









share|improve this question



















  • 2




    How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
    – Panki
    Nov 26 at 9:58















up vote
0
down vote

favorite












I have this output in the following and I am trying to convert it to JSON api format. I want to know how can I do it.



 rock64@rockpro64:~$ sh MACscript.sh 
eth0
11:1d:11:11:11:1d
lo
00:00:00:00:00:00


Do I have to use python script or can I do it using shell script?



This is my MACshell script:



 rock64@rockpro64:~$ cat MACscript.sh 
!/bin/bash
getmacifup.sh: Print active NICs MAC addresses
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q unknown $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done









share|improve this question



















  • 2




    How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
    – Panki
    Nov 26 at 9:58













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this output in the following and I am trying to convert it to JSON api format. I want to know how can I do it.



 rock64@rockpro64:~$ sh MACscript.sh 
eth0
11:1d:11:11:11:1d
lo
00:00:00:00:00:00


Do I have to use python script or can I do it using shell script?



This is my MACshell script:



 rock64@rockpro64:~$ cat MACscript.sh 
!/bin/bash
getmacifup.sh: Print active NICs MAC addresses
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q unknown $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done









share|improve this question















I have this output in the following and I am trying to convert it to JSON api format. I want to know how can I do it.



 rock64@rockpro64:~$ sh MACscript.sh 
eth0
11:1d:11:11:11:1d
lo
00:00:00:00:00:00


Do I have to use python script or can I do it using shell script?



This is my MACshell script:



 rock64@rockpro64:~$ cat MACscript.sh 
!/bin/bash
getmacifup.sh: Print active NICs MAC addresses
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q unknown $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done






shell python json ifconfig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Nov 26 at 9:19









Rakib Fiha

114




114







  • 2




    How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
    – Panki
    Nov 26 at 9:58













  • 2




    How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
    – Panki
    Nov 26 at 9:58








2




2




How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
– Panki
Nov 26 at 9:58





How is the JSON supposed to look? What are the names of the fields? Take a look at this: stedolan.github.io/jq
– Panki
Nov 26 at 9:58











2 Answers
2






active

oldest

votes

















up vote
3
down vote













With plain bash you could do:



json=$(
sh MACscript.sh |
pairs=()
while read interface; read ether; do
pairs+=(""$interface":"$ether"")
done
IFS=,
echo "$pairs[*]"

)
echo "$json"


outputs



"eth0":"11:1d:11:11:11:1d","lo":"00:00:00:00:00:00"





share|improve this answer






















  • Wow, this solution is just perfect. Thank you
    – Rakib Fiha
    Nov 27 at 2:27

















up vote
0
down vote













you can use various ways to get your json values. bash, python,perl,.....

you can find useful posts about these in this website. however here is an example:



 arr1=($( ls /sys/class/net))
arr2=($( cat /sys/class/net/*/address ))

vars1=($arr1[@])
vars2=($arr2[@])
len=$#arr1[@]

printf "n"
printf "t"'"data"'":[n"

for (( i=0; i<len; i+=1 ))
do
printf "t "'"#interface"'":"$vars1[i]",t"'"#address"'":"$vars2[i]"
"

if [ $i -lt $((len-1)) ] ; then
printf ",n"
fi
done
printf "n"
printf "t]n"
printf "n"
echo


output:




"data":[
"#interface":"eth0", "#address":"00:50:56:a9:c0:81" ,
"#interface":"lo", "#address":"00:00:00:00:00:00"
]



and you can use this website to validate your json: https://codebeautify.org/online-json-editor






share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484149%2fhow-to-convert-shell-output-to-json%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    With plain bash you could do:



    json=$(
    sh MACscript.sh |
    pairs=()
    while read interface; read ether; do
    pairs+=(""$interface":"$ether"")
    done
    IFS=,
    echo "$pairs[*]"

    )
    echo "$json"


    outputs



    "eth0":"11:1d:11:11:11:1d","lo":"00:00:00:00:00:00"





    share|improve this answer






















    • Wow, this solution is just perfect. Thank you
      – Rakib Fiha
      Nov 27 at 2:27














    up vote
    3
    down vote













    With plain bash you could do:



    json=$(
    sh MACscript.sh |
    pairs=()
    while read interface; read ether; do
    pairs+=(""$interface":"$ether"")
    done
    IFS=,
    echo "$pairs[*]"

    )
    echo "$json"


    outputs



    "eth0":"11:1d:11:11:11:1d","lo":"00:00:00:00:00:00"





    share|improve this answer






















    • Wow, this solution is just perfect. Thank you
      – Rakib Fiha
      Nov 27 at 2:27












    up vote
    3
    down vote










    up vote
    3
    down vote









    With plain bash you could do:



    json=$(
    sh MACscript.sh |
    pairs=()
    while read interface; read ether; do
    pairs+=(""$interface":"$ether"")
    done
    IFS=,
    echo "$pairs[*]"

    )
    echo "$json"


    outputs



    "eth0":"11:1d:11:11:11:1d","lo":"00:00:00:00:00:00"





    share|improve this answer














    With plain bash you could do:



    json=$(
    sh MACscript.sh |
    pairs=()
    while read interface; read ether; do
    pairs+=(""$interface":"$ether"")
    done
    IFS=,
    echo "$pairs[*]"

    )
    echo "$json"


    outputs



    "eth0":"11:1d:11:11:11:1d","lo":"00:00:00:00:00:00"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 at 11:51

























    answered Nov 26 at 11:45









    glenn jackman

    49.6k569106




    49.6k569106











    • Wow, this solution is just perfect. Thank you
      – Rakib Fiha
      Nov 27 at 2:27
















    • Wow, this solution is just perfect. Thank you
      – Rakib Fiha
      Nov 27 at 2:27















    Wow, this solution is just perfect. Thank you
    – Rakib Fiha
    Nov 27 at 2:27




    Wow, this solution is just perfect. Thank you
    – Rakib Fiha
    Nov 27 at 2:27












    up vote
    0
    down vote













    you can use various ways to get your json values. bash, python,perl,.....

    you can find useful posts about these in this website. however here is an example:



     arr1=($( ls /sys/class/net))
    arr2=($( cat /sys/class/net/*/address ))

    vars1=($arr1[@])
    vars2=($arr2[@])
    len=$#arr1[@]

    printf "n"
    printf "t"'"data"'":[n"

    for (( i=0; i<len; i+=1 ))
    do
    printf "t "'"#interface"'":"$vars1[i]",t"'"#address"'":"$vars2[i]"
    "

    if [ $i -lt $((len-1)) ] ; then
    printf ",n"
    fi
    done
    printf "n"
    printf "t]n"
    printf "n"
    echo


    output:




    "data":[
    "#interface":"eth0", "#address":"00:50:56:a9:c0:81" ,
    "#interface":"lo", "#address":"00:00:00:00:00:00"
    ]



    and you can use this website to validate your json: https://codebeautify.org/online-json-editor






    share|improve this answer
























      up vote
      0
      down vote













      you can use various ways to get your json values. bash, python,perl,.....

      you can find useful posts about these in this website. however here is an example:



       arr1=($( ls /sys/class/net))
      arr2=($( cat /sys/class/net/*/address ))

      vars1=($arr1[@])
      vars2=($arr2[@])
      len=$#arr1[@]

      printf "n"
      printf "t"'"data"'":[n"

      for (( i=0; i<len; i+=1 ))
      do
      printf "t "'"#interface"'":"$vars1[i]",t"'"#address"'":"$vars2[i]"
      "

      if [ $i -lt $((len-1)) ] ; then
      printf ",n"
      fi
      done
      printf "n"
      printf "t]n"
      printf "n"
      echo


      output:




      "data":[
      "#interface":"eth0", "#address":"00:50:56:a9:c0:81" ,
      "#interface":"lo", "#address":"00:00:00:00:00:00"
      ]



      and you can use this website to validate your json: https://codebeautify.org/online-json-editor






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        you can use various ways to get your json values. bash, python,perl,.....

        you can find useful posts about these in this website. however here is an example:



         arr1=($( ls /sys/class/net))
        arr2=($( cat /sys/class/net/*/address ))

        vars1=($arr1[@])
        vars2=($arr2[@])
        len=$#arr1[@]

        printf "n"
        printf "t"'"data"'":[n"

        for (( i=0; i<len; i+=1 ))
        do
        printf "t "'"#interface"'":"$vars1[i]",t"'"#address"'":"$vars2[i]"
        "

        if [ $i -lt $((len-1)) ] ; then
        printf ",n"
        fi
        done
        printf "n"
        printf "t]n"
        printf "n"
        echo


        output:




        "data":[
        "#interface":"eth0", "#address":"00:50:56:a9:c0:81" ,
        "#interface":"lo", "#address":"00:00:00:00:00:00"
        ]



        and you can use this website to validate your json: https://codebeautify.org/online-json-editor






        share|improve this answer












        you can use various ways to get your json values. bash, python,perl,.....

        you can find useful posts about these in this website. however here is an example:



         arr1=($( ls /sys/class/net))
        arr2=($( cat /sys/class/net/*/address ))

        vars1=($arr1[@])
        vars2=($arr2[@])
        len=$#arr1[@]

        printf "n"
        printf "t"'"data"'":[n"

        for (( i=0; i<len; i+=1 ))
        do
        printf "t "'"#interface"'":"$vars1[i]",t"'"#address"'":"$vars2[i]"
        "

        if [ $i -lt $((len-1)) ] ; then
        printf ",n"
        fi
        done
        printf "n"
        printf "t]n"
        printf "n"
        echo


        output:




        "data":[
        "#interface":"eth0", "#address":"00:50:56:a9:c0:81" ,
        "#interface":"lo", "#address":"00:00:00:00:00:00"
        ]



        and you can use this website to validate your json: https://codebeautify.org/online-json-editor







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 at 10:11









        BlackCrystal

        16911




        16911



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484149%2fhow-to-convert-shell-output-to-json%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?