# Nesting json\_select\_object into two json\_foreach loops

**URL:** <https://community.mp3tag.de/t/nesting-json-select-object-into-two-json-foreach-loops/72123>\
**Category:** Fixed Bugs\
**Tags:** bug-fixed\
**Created:** [November 25, 2026, 5:56pm UTC](https://community.mp3tag.de/t/nesting-json-select-object-into-two-json-foreach-loops/72123 "2026-11-25T17:56:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yorickausyps](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/y/f6c823/32.png) [@yorickausyps](https://community.mp3tag.de/u/yorickausyps)\
**Post date:** [November 25, 2026, 5:56pm UTC](https://community.mp3tag.de/t/nesting-json-select-object-into-two-json-foreach-loops/72123/1 "2026-11-25T17:56:59Z")

</div>

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":

```auto
{"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:

```auto
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:

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)\
**Post date:** [November 26, 2026, 4:18pm UTC](https://community.mp3tag.de/t/nesting-json-select-object-into-two-json-foreach-loops/72123/2 "2026-11-26T16:18:43Z")

</div>

This is fixed with [Mp3tag v3.37.1-beta.2](https://community.mp3tag.de/t/455). Thanks for reporting!
