RHEL/Centos7 Shell tips2 -- json parser through sed

lake@localhost:~/tmp/huaqing$ cat    shell-json.sh
#!/bin/bash  

### try to get url link after url lable in json string through shell/sed 


example_json={"rv":0,flag:1,"url":"http://www.xxx.com","msg":"test"}  
  
parse_json(){  
  
echo "${1//\"/}" | sed "s/.*$2:\([^,}]*\).*/\1/"
  
}  
  
  
  
echo $example_json  
  
value=$(parse_json $example_json "url")  
  
echo "url is "$value  
lake@localhost:~/tmp/huaqing$ 
lake@localhost:~/tmp/huaqing$ sh shell-json.sh 
{rv:0,flag:1,url:http://www.xxx.com,msg:test}
url is http://www.xxx.com
lake@localhost:~/tmp/huaqing$ 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章