[F] bug with json_foreach

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/... 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.

Hi mp3taglover

fully right we need a

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

does any one has some syntax for us?!

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.

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 !!!.
    # 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>

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).

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