In json web sources a json element often resides several object levels below the current level. To reach such a deeply nested element and read its value, many lines of code are needed. This process even gets more complex, if it's not sure, whether all the intermediate object levels exist or if the chain stops at some point.
Therefore I came up with the following code for reading a value that is e.g. two levels below the current object level, and that is robust and fail save against the hierarchy possibly ending at an intermediate level.
json_select_object "links"
ifnot ""
json_select_object "target"
ifnot ""
json_select "href"
json_unselect_object
endif
json_unselect_object
endif
With an extended json_select command that supports the specification of a variable number of parent object names in addition to the elements name to be read, e.g.
json_select "parent1" "parent2 "more parents" "object"
the above script would then need only one line:
json_select "links" "target" "href"
This extended command should
- support a variable number of hierarchical parent levels, probably limited up to 10.
- give nothing back, if any of the parents or the element itself does not exist or if the element has no value, otherwise it returns the elements value.
- in any case stay at the current object level.
In my use case I only have two hierarchical object levels, but e.g. in the script [WS] iTunes source script with multiple search criteria by @rboss you can find up to four levels.
In my opinion this new feature would greatly simplyfy coding, make scripts more robust and failsave against changes on the web source server side and probably increase execution speed of scripts.