Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:guide-developer:jshn [2020/01/13 12:05] – [jsonfilter] stokito | docs:guide-developer:jshn [2024/08/03 12:52] (current) – [See also] stokito | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| # generating json data | # generating json data | ||
| json_init | json_init | ||
| + | json_add_int " | ||
| json_add_string " | json_add_string " | ||
| json_add_object " | json_add_object " | ||
| json_add_int " | json_add_int " | ||
| json_close_object | json_close_object | ||
| - | MSG=`json_dump` | + | MSG_JSON=`json_dump` |
| - | # MSG now contains: { " | + | # the MSG_JSON var now contains: { " |
| - | # parsing json data | + | # parsing json data from the MSG_JSON variable |
| - | json_load "$MSG" | + | local var1 code msg # declare local variables to load data into |
| - | json_select test | + | json_load "$MSG_JSON" |
| - | json_get_var var1 testdata | + | json_select test # go into the object inside of the " |
| - | json_select .. | + | json_get_var var1 testdata |
| - | json_get_var var2 msg | + | json_select .. # go back to the upper level |
| - | echo "msg: $var2 - testdata: $var1" | + | # load the " |
| + | json_get_vars code msg | ||
| + | |||
| + | echo "code: $code, | ||
| </ | </ | ||
| Line 71: | Line 75: | ||
| ==== More complicated examples ==== | ==== More complicated examples ==== | ||
| + | === Parse file example === | ||
| Given the file: | Given the file: | ||
| <code - / | <code - / | ||
| Line 100: | Line 104: | ||
| json_get_var last_seen last_seen | json_get_var last_seen last_seen | ||
| </ | </ | ||
| + | |||
| + | === Parse arrays example === | ||
| Given the file: | Given the file: | ||
| Line 105: | Line 111: | ||
| { | { | ||
| " | " | ||
| - | " | + | " |
| - | " | + | |
| } | } | ||
| } | } | ||
| Line 120: | Line 125: | ||
| json_select " | json_select " | ||
| - | if json_get_type Type lan && [ " | + | if json_is_a |
| then | then | ||
| - | json_select lan | + | |
| - | local idx="1" | + | idx=1 # note that array element position starts from 1 not, 0 |
| - | while | + | while json_is_a |
| - | do | + | do |
| - | json_get_var ip_addr $idx | + | json_get_var ip_addr $idx |
| - | echo " | + | echo " |
| - | $((idx++)) 2> /dev/null | + | idx=$(( idx + 1 )) |
| - | done | + | done |
| fi | fi | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Parse list of objects === | ||
| + | |||
| + | The example will download electricity hourly prices for Finland in JSON and parse it. | ||
| + | The prices JSON looks like | ||
| + | <code - prices.json> | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | ] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Script to parse it: | ||
| + | <code bash> | ||
| + | #!/bin/sh | ||
| + | set -e | ||
| + | . / | ||
| + | date=$(date -u +%Y-%m-%dT%H: | ||
| + | |||
| + | # download prices JSON via wget in quiet mode with output to stdout that will be saved to a variable PRICES_JSON | ||
| + | PRICES_JSON=$(wget -qO - " | ||
| + | exit_status=$? | ||
| + | # check exit code: if any error then exit | ||
| + | if [ $exit_status -ne 0 ]; then | ||
| + | >& | ||
| + | exit $exit_status | ||
| + | fi | ||
| + | |||
| + | json_load " | ||
| + | json_select " | ||
| + | idx=1 # note that array element position starts from 1 not, 0 | ||
| + | # iterate over data inside " | ||
| + | while json_is_a $idx object | ||
| + | do | ||
| + | json_select | ||
| + | # now parse {" | ||
| + | json_get_var price_date " | ||
| + | echo " | ||
| + | json_get_var price_value " | ||
| + | echo " | ||
| + | idx=$(( idx + 1 )) | ||
| + | json_select .. # go back to the upper level to the prices array | ||
| + | done | ||
| + | |||
| + | echo "Total parsed $idx" | ||
| + | </ | ||
| + | |||
| + | ===== json_for_each_item ===== | ||
| + | Function useful to iterate through the different elements of an | ||
| + | array or object; the provided callback function is called for each | ||
| + | element which is passed the value, key and user provided arguments. | ||
| + | For field types different from array or object the callback is called | ||
| + | with the retrieved value. | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/sh | ||
| + | . / | ||
| + | |||
| + | json_load_file /data2.json | ||
| + | |||
| + | dump_item() { | ||
| + | echo "item: $1 ' | ||
| + | } | ||
| + | |||
| + | json_for_each_item " | ||
| + | </ | ||
| + | |||
| + | ===== json_get_values ===== | ||
| + | To get all values of an array use the '' | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/sh | ||
| + | . / | ||
| + | |||
| + | json_load ' | ||
| + | json_get_values values " | ||
| + | echo " | ||
| + | # print comma separated | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Get all fields into variables ===== | ||
| + | Get all fields and declare variables for them: | ||
| + | <code bash> | ||
| + | json_load ' | ||
| + | json_select " | ||
| + | json_get_keys keys | ||
| + | for key in $keys | ||
| + | do | ||
| + | json_get_var " | ||
| + | done | ||
| + | echo " | ||
| + | echo " | ||
| </ | </ | ||
| Line 136: | Line 245: | ||
| Internally the ''/ | Internally the ''/ | ||
| - | You can use it directly: | ||
| - | |||
| < | < | ||
| root@OpenWrt:/# | root@OpenWrt:/# | ||
| Usage: jshn [-n] [-i] -r < | Usage: jshn [-n] [-i] -r < | ||
| - | root@OpenWrt:/# | + | </ |
| + | |||
| + | Options: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | You can call it directly: | ||
| + | |||
| + | < | ||
| + | root@OpenWrt:/# | ||
| json_init; | json_init; | ||
| json_add_object ' | json_add_object ' | ||
| Line 158: | Line 280: | ||
| json_close_object; | json_close_object; | ||
| </ | </ | ||
| + | The output is then evaluated inside of the shell script to create in memory structure as in file. | ||
| - | Options: | + | If you created an object like: |
| - | * '' | + | <code> |
| - | * '' | + | json_init; |
| - | | + | json_add_string |
| - | * '' | + | json_dump; |
| - | * '' | + | </code> |
| - | | + | Then internally it will call '' |
| - | * '' | + | |
| - | + | ||
| + | < | ||
| + | root@OpenWrt:/# | ||
| + | { " | ||
| + | </ | ||
| + | Here '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| ===== Other examples ===== | ===== Other examples ===== | ||
| Line 274: | Line 403: | ||
| ==== jsonfilter ==== | ==== jsonfilter ==== | ||
| - | The '' | + | The [[commit> |
| < | < | ||
| root@openwrt: | root@openwrt: | ||
| - | jsonfilter: unrecognized option: - | ||
| == Usage == | == Usage == | ||
| Line 326: | Line 454: | ||
| ==== jq ==== | ==== jq ==== | ||
| - | [[https:// | + | [[https:// |
| - | You can install | + | It's not installed by default in OpenWRT because is too big (more than 200Kb) so to install |
| By default it just colorize an output e.g. '' | By default it just colorize an output e.g. '' | ||
| + | |||
| + | ==== See also ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||