天涼好個秋.

OpenWrt jsonfilter

3083 阅 0 评 IT手札

Info

root@Openwrt:~# opkg files jsonfilter
Package jsonfilter (2018-02-04-c7e938d6-1) is installed on root and has the following files:
/usr/bin/jsonfilter
root@Openwrt:~# opkg info jsonfilter
Package: jsonfilter
Version: 2018-02-04-c7e938d6-1
Depends: libc, libubox, libjson-c
Status: install user installed
Architecture: x86_64
Installed-Time: 1528614208
root@Openwrt:~# jsonfilter
== Usage ==
  # jsonfilter [-a] [-i <file> | -s "json..."] {-t <pattern> | -e <pattern>}
  -q            Quiet, no errors are printed
  -h, --help    Print this help
  -a            Implicitely treat input as array, useful for JSON logs
  -i path       Specify a JSON file to parse
  -s "json"     Specify a JSON string to parse
  -l limit      Specify max number of results to show
  -F separator  Specify a field separator when using export
  -t <pattern>  Print the type of values matched by pattern
  -e <pattern>  Print the values matched by pattern
  -e VAR=<pat>  Serialize matched value for shell "eval"

== Patterns ==

  Patterns are JsonPath: http://goessner.net/articles/JsonPath/
  This tool implements $, @, [], * and the union operator ','
  plus the usual expressions and literals.
  It does not support the recursive child search operator '..' or
  the '?()' and '()' filter expressions as those would require a
  complete JavaScript engine to support them.

== Examples ==

  Display the first IPv4 address on lan:
  # ifstatus lan | jsonfilter -e '@["ipv4-address"][0].address'

  Extract the release string from the board information:
  # ubus call system board | jsonfilter -e '@.release.description'

  Find all interfaces which are up:
  # ubus call network.interface dump | \
        jsonfilter -e '@.interface[@.up=true].interface'

  Export br-lan traffic counters for shell eval:
  # devstatus br-lan | jsonfilter -e 'RX=@.statistics.rx_bytes' \
        -e 'TX=@.statistics.tx_bytes'

Some example

Option -a

root@Openwrt:~# echo 1 2 3 4 | jsonfilter -a -e "$"
[ 1, 2, 3, 4 ]

Option -i

root@Openwrt:~# echo '{"a":1,"b":2}' > /tmp/test.json;jsonfilter -i /tmp/test.json -e "$"
{ "a": 1, "b": 2 }

Option -s

root@Openwrt:~# jsonfilter -s '{"a":1,"b":2}' -e "$"
{ "a": 1, "b": 2 }

Get Bing pic url from api

root@Openwrt:~# curl -k 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'
{"images":[{"startdate":"20180706","fullstartdate":"201807061600","enddate":"20180707","url":"/az/hprichbg/rb/Peloton_ZH-CN7472605035_1920x1080.jpg","urlbase":"/az/hprichbg/rb/Peloton_ZH-CN7472605035","copyright":"2016年环法自行车赛中骑在绿树成荫的道路上的选手 (© Michael Steele/Getty Images Sport)","copyrightlink":"http://www.bing.com/search?q=%E7%8E%AF%E6%B3%95%E8%87%AA%E8%A1%8C%E8%BD%A6%E8%B5%9B&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20180706_Peloton%22&FORM=HPQUIZ","wp":false,"hsh":"f4e4025f39bd7ed187dee34e042e8ee6","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}
root@Openwrt:~# curl -sk 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'  | jsonfilter -e "$.images[0].url"
/az/hprichbg/rb/Peloton_ZH-CN7472605035_1920x1080.jpg

which elemnt a's value is 1 in array

root@Openwrt:~# jsonfilter -s '[{"a":"1","b":100},{"a":"2","b":200}]' -e "$[@.a='1']"
EOF