How to convert shell output to JSON?
Clash 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
shell python json ifconfig
add a comment |
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
shell python json ifconfig
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
add a comment |
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
shell python json ifconfig
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
shell python json ifconfig
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
add a comment |
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
add a comment |
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"
Wow, this solution is just perfect. Thank you
– Rakib Fiha
Nov 27 at 2:27
add a comment |
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
add a comment |
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"
Wow, this solution is just perfect. Thank you
– Rakib Fiha
Nov 27 at 2:27
add a comment |
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"
Wow, this solution is just perfect. Thank you
– Rakib Fiha
Nov 27 at 2:27
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 26 at 10:11
BlackCrystal
16911
16911
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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