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/14 20:39] – Add [json_get_values] description stokitodocs: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 "code" "0"
 json_add_string "msg" "Hello, world!" json_add_string "msg" "Hello, world!"
 json_add_object "test" json_add_object "test"
 json_add_int "testdata" "1" json_add_int "testdata" "1"
 json_close_object json_close_object
-MSG=`json_dump` +MSG_JSON=`json_dump` 
-MSG now contains: { "msg": "Hello, world!", "test": { "testdata": 1 } }+the MSG_JSON var now contains: { "code": 0, "msg": "Hello, world!", "test": { "testdata": 1 } }
  
  
-# 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 "test" field 
-json_select .. +json_get_var var1 testdata # first is var name "var1" then the JSON field name "testdata" 
-json_get_var var2 msg +json_select .. # go back to the upper level 
-echo "msg: $var2 - testdata: $var1"+# load the "code" field into corresponding code var, and the "msg" field into the msg var  
 +json_get_vars code msg 
 +  
 +echo "code: $code, msg: $msg, testdata: $var1"
 </code> </code>
  
Line 71: Line 75:
  
 ==== More complicated examples ==== ==== More complicated examples ====
 +=== Parse file example ===
 Given the file: Given the file:
 <code - /data1.json> <code - /data1.json>
Line 101: Line 105:
 </code> </code>
  
-Parse arrays examзle. Given the file:+=== Parse arrays example === 
 + 
 +Given the file:
 <code - /data2.json> <code - /data2.json>
 { {
Line 122: 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 132: 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 399: 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/14 20:39
  • by stokito