Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:guide-developer:jshn [2023/08/26 15:10] – add json_get_vars stokitodocs:guide-developer:jshn [2024/08/03 12:52] (current) – [See also] stokito
Line 75: Line 75:
  
 ==== More complicated examples ==== ==== More complicated examples ====
 +=== Parse file example ===
 Given the file: Given the file:
 <code - /data1.json> <code - /data1.json>
Line 105: Line 105:
 </code> </code>
  
-Parse arrays examзle. Given the file:+=== Parse arrays example === 
 + 
 +Given the file:
 <code - /data2.json> <code - /data2.json>
 { {
Line 126: Line 128:
 then then
   json_select lan   json_select lan
-  idx=1+  idx=1 # note that array element position starts from 1 not, 0
   while json_is_a ${idx} string  ## iterate over data inside "lan" object   while json_is_a ${idx} string  ## iterate over data inside "lan" object
   do   do
Line 136: Line 138:
 </code> </code>
  
 +
 +=== 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>
 +{
 +  "prices": [
 +    {
 +      "date": "2024-08-03T11:00:00.000Z",
 +      "value": 6.41
 +    },
 +    {
 +      "date": "2024-08-03T12:00:00.000Z",
 +      "value": 4.1
 +    },
 +  ]
 +}
 +</code>
 +
 +Script to parse it:
 +<code bash>
 +#!/bin/sh
 +set -e
 +. /usr/share/libubox/jshn.sh
 +date=$(date -u +%Y-%m-%dT%H:00:00.000Z)
 +
 +# 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 - "https://sahkotin.fi/prices?start=$date")
 +exit_status=$?
 +# check exit code: if any error then exit
 +if [ $exit_status -ne 0 ]; then
 +   >&2 echo "error $exit_status"
 +   exit $exit_status
 +fi
 +
 +json_load "$PRICES_JSON"
 +json_select "prices"
 +idx=1 # note that array element position starts from 1 not, 0
 +# iterate over data inside "price" array until elements are objects
 +while json_is_a $idx object
 +do
 +  json_select  $idx
 +  # now parse {"date": "2024-08-04T21:00:00.000Z", "value": 22.58}
 +  json_get_var price_date "date"
 +  echo "price_date: $price_date"
 +  json_get_var price_value "value"
 +  echo "price_value: $price_value"
 +  idx=$(( idx + 1 ))
 +  json_select .. # go back to the upper level to the prices array
 +done
 +
 +echo "Total parsed $idx"
 +</code>
  
 ===== json_for_each_item ===== ===== json_for_each_item =====
Line 403: Line 459:
  
 ==== See also ==== ==== See also ====
-* [[https://github.com/burningtree/awesome-json|Awesome JSON]] A curated list of awesome JSON libraries and resources.+  * [[https://github.com/burningtree/awesome-json|Awesome JSON]] A curated list of awesome JSON libraries and resources. 
 +  * [[https://github.com/yurt-page/openwrt-script-samples|OpenWrt script samples]] 
 +  * [[https://forum.openwrt.org/t/writing-shell-scripts-support-topic/206144|Writing shell scripts support forum topic]]
  
  
  
  
  • Last modified: 2023/08/26 15:10
  • by stokito