The MusicBrainz API often nests JSON arrays and objects into each other. To process such structures, it is therefore necessary to use correspondingly nested json_foreach and json_select_object parser commands.
The following JSON object contains an example of an inner array "inside", nested in the object "container" and that object is also nested in an outer array "outside":
{"outside":[{"title":"Title of Array Outside","container":{"title":"Title of Container","inside":[{"title":"Title of Array Inside"}]}}]}
This JSON object needs for parsing it an inner json_foreach loop, nested in a json_select_object and json_unselect_object pair, which is nested in an outer json_foreach loop. See the following example:
use "{\"outside\":[{\"title\":\"Title of Array Outside\",\"container\":{\"title\":\"Title of Container\",\"inside\":[{\"title\":\"Title of Array Inside\"}]}}]}"
json "ON" "current"
json_foreach "outside"
json_select_object "container"
json_foreach "inside"
json_foreach_end
outputto "4:"
say "In Object Container: "
json_select "title"
sayrest
json_unselect_object
json_foreach_end
The script produces the following output:
output["4:"]= "In Object Container: Title of Array Outside"
The issue is here, that at the end of the inner json_foreach loop the json_foreach_end also effectively unselects the object "container", that encloses it. Output #4 shows the title of the outer array, but it should show the title of the object it is in. In the debug output file the object "container" is still shown to be selected but json_select fetches the object "title" from the outside array.
Command : json_select
Parameter 1 : >title<
JSON objects : >{container}<
JSON loops : >outside: 1/1<
Output : >In Object Container: <
Line and position:
Title of Array Outside
To trigger this issue, both inner and outer arrays json_foreach loops must be present. With only one inner array you could avoid it by rearranging the code and process the loop directly before unselecting the enclosing object.
There is no workaround I know of, if you have more than one inner array.