# \[F\] bug with json\_foreach

**URL:** https://community.mp3tag.de/t/f-bug-with-json-foreach/16851
**Category:** Fixed Bugs
**Tags:** bug-fixed
**Created:** [April 25, 2015, 11:25am UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851 "2015-04-25T11:25:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mp3taglover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@mp3taglover](https://community.mp3tag.de/u/mp3taglover)
#### Post date: [April 25, 2015, 11:25am UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/1 "2015-04-25T11:25:46Z")

</div>

there is a bug with standard coverurl setting, if discogs release has multiple covers it adds ALL urls into coverurl.. (e.g. in coverurl you'll get: [http://api-img.discogs.com/....http://api-....discogs.com/.](http://api-img.discogs.com/....http://api-img.discogs.com/....http://api-img.discogs.com/.).. instead of just 1 URL)

here is a code:

```
# Coverurl
outputto "coverurl"
json_foreach "images"
    json_select "type"
    if "secondary"
        json_select "uri"
        sayrest
    endif
json_foreach_end

```

there is no way to escape from the loop ...

maybe add a break statement for the loop? would be handy thx.

---

<div class="post-metadata">

### Author: ![Leftaf](https://community.mp3tag.de/user_avatar/community.mp3tag.de/leftaf/32/7767_2.png) [@Leftaf](https://community.mp3tag.de/u/Leftaf)
#### Post date: [May 9, 2015, 4:08pm UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/2 "2015-05-09T16:08:58Z")

</div>

Hi mp3taglover

fully right we need a

```
do {
    code block to be executed
}
while (condition);

```

does any one has some syntax for us?!

---

<div class="post-metadata">

### Author: ![mp3taglover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@mp3taglover](https://community.mp3tag.de/u/mp3taglover)
#### Post date: [May 9, 2015, 5:22pm UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/3 "2015-05-09T17:22:33Z")

</div>

QUOTE (Leftaf @ May 9 2015, 21:08) \<{POST\_SNAPBACK}\>

Hi mp3taglover

fully right we need a

```
do {
    code block to be executed
}
while (condition);

```

does any one has some syntax for us?!

well, there is actually

```
Do ... While S n Execute the command surrounded by the two commands while S occurs on current position. The optional second parameter limits the execution of the loop to maximal n times.
Do
MoveLine 1
While "<td>"
Nesting of Do commands is not allowed.

```

but nesting is not allowed and the thing is you can't use this in json mode, because it's suitable only for NON json mode..

also there is another bug with json\_foreach, you can't just use multiple nested json\_foreach things if you go through some nested array.

e.g.:

```
# Credits per track
json_foreach "tracklist"
    json_select "type_"
    if "track"
        # ARTIST
        outputto "ARTISTTMP"
        json_select_many "artists" "name" ", "
                ifnot ""
            json_foreach "artists"
                json_select "name"
                ifnot ""        
                    say "<name>"
                    sayrest
                    say "</name>"
                endif
                json_select "anv"
                ifnot ""
                    say "<anv>"
                    sayrest
                    say "</anv>"
                endif
                json_select "join"
                ifnot ""
                    say "<join>"
                    sayrest
                    say "</join>"
                endif
            json_foreach_end
        else
            outputto "ARTISTTMP"
            sayoutput "BANDTMP"
        endif
        say "|"    

        outputto "CRTMP"
        json_select_many "extraartists" "name" ", "
        ifnot ""
            json_foreach "extraartists"            
                json_select "name"
                ifnot ""
                    say "<name>"
                sayrest
                    say "</name>"
                endif
                
                json_select "anv"
                ifnot ""
                    say "<anv>"
                    sayrest
                    say "</anv>"
                endif

                json_select "role"
                ifnot ""
                    say "<role>"
                    sayrest
                    say "</role>"
                endif
                
                json_select "tracks"
                ifnot ""
                    say "<tracks>"
                    sayrest
                    say "</tracks>"
                endif
            json_foreach_end
        endif
        say "|"
    endif
json_foreach_end

```

in this case only ARTISTTMP will be filled correctly with artists and CRTMP will be with root extraartists instead of the extraartists nested for each track, it's all because json\_foreach\_end goes to the root of the array after finishing i think instead of just going to the same element which was selected first.

the only workaround i've found is to put another json\_foreach over tracklist again and put there json\_foreach related to CRTMP:

```
# Credits per track
json_foreach "tracklist"
    json_select "type_"
    if "track"
        outputto "CRTMP"
        json_select_many "extraartists" "name" ", "
        ifnot ""
            json_foreach "extraartists"            
                json_select "name"
                ifnot ""
                    say "<name>"
                sayrest
                    say "</name>"
                endif
                
                json_select "anv"
                ifnot ""
                    say "<anv>"
                    sayrest
                    say "</anv>"
                endif

                json_select "role"
                ifnot ""
                    say "<role>"
                    sayrest
                    say "</role>"
                endif
                
                json_select "tracks"
                ifnot ""
                    say "<tracks>"
                    sayrest
                    say "</tracks>"
                endif
            json_foreach_end
        endif
        say "|"
    endif
json_foreach_end

```

there should be either some 'break' function to break out of cycle or jump/goto.

---

<div class="post-metadata">

### Author: ![vkostas](https://community.mp3tag.de/user_avatar/community.mp3tag.de/vkostas/32/227_2.png) [@vkostas](https://community.mp3tag.de/u/vkostas)
#### Post date: [May 16, 2015, 6:07pm UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/4 "2015-05-16T18:07:48Z")

</div>

> [@mp3taglover](#):
>
> there is a bug with standard coverurl setting, if discogs release has multiple covers it adds ALL urls into coverurl.. (e.g. in coverurl you'll get: [http://api-img.discogs.com/....http://api-....discogs.com/](http://api-img.discogs.com/....http://api-img.discogs.com/....http://api-img.discogs.com/)... instead of just 1 URL)
> 
> ...........  
> there is no way to escape from the loop ...
> 
> maybe add a break statement for the loop? would be handy thx.

hm, I don't think this is a bug even I am a lot in favor of it. Bug is considered smth delivered not working as described. This is a missing functionality, yes.  
There are a couple of solutions Florian could apply unless he finds smth better.

1. New command **readoutputto ""** It would bring the value of tag already filled before (after **outputto ""** ) to the working area. I have requested several times this feature (which I think will increase the power of WS) but I have not seen any reaction yet. Last time requested in this post [[WS] HELP !!!](https://community.mp3tag.de/t/16672/6).

```
# Coverurl
outputto "coverurl"
json_foreach "images"
    json_select "type"
    if "secondary"
        readoutputto "coverurl" # <<<<<<<<< new code
        if "" # <<<<<<<<< new code
            json_select "uri"
            sayrest
        endif # <<<<<<<<< new code
    endif
json_foreach_end

```

2. New command **json\_foreach\_exit** (as you mentioned) It fits your example but could be insufficient for other cases. E.g. fill the tag only the first time but do not leave the loop.

```
# Coverurl
outputto "coverurl"
json_foreach "images"
    json_select "type"
    if "secondary"
        json_select "uri"
        sayrest
        json_forech_exit # <<<<<<<<< new code
    endif
json_foreach_end</li></ol>

```

---

<div class="post-metadata">

### Author: ![mp3taglover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@mp3taglover](https://community.mp3tag.de/u/mp3taglover)
#### Post date: [May 17, 2015, 10:09am UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/5 "2015-05-17T10:09:37Z")

</div>

> [@Victor Kostas](#):
>
> hm, I don't think this is a bug even I am a lot in favor of it. Bug is considered smth delivered not working as described. This is a missing functionality, yes

Well, I've desribed here 2 separate bugs.

Both with loop functionality.

One of the bugs is you can't stop the loop, and second when you have nested loops inside the loop 2nd nested loop is going through root element instead of the parent element (which was selected).

---

<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: [December 28, 2018, 2:08pm UTC](https://community.mp3tag.de/t/f-bug-with-json-foreach/16851/6 "2018-12-28T14:08:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
