How can I keep leading and trailing spaces in a parsed string?

I'm still learning the syntax of the Web Source language.

If I parse a JSON result, I get a string called JOINPHRASE which can include leading and trailing spaces like in the string " feat. " (which would be space feat. space)

You can see that correct result - including the leading space - in the script line debug output for line 391.
The code snippet starting with line 391:

                    json_select "JOINPHRASE"
                    ifnot ""
                        GotoChar 1
                        sayrest
                        say " "
                    endif                    
                json_foreach_end
                say "|"

Debug output:

------------------------------------------------------------
Script-Line    : 391
Command        : json_select
Parameter 1    : >joinphrase<

Output         : >Olivia Rodrigo|The Kid LAROI<

Line and position:
 feat. 
^
------------------------------------------------------------
Script-Line    : 392
Command        : ifnot
Output         : >Olivia Rodrigo|The Kid LAROI<

Line and position:
 feat. 
^

------------------------------------------------------------

Script-Line    : 105
Command        : gotochar
Parameter 1    : >1<

Output         : >Olivia Rodrigo|The Kid LAROI<

Line and position:
 feat. 
^

------------------------------------------------------------

Script-Line    : 106
Command        : sayrest

Output         : >Olivia Rodrigo|The Kid LAROIfeat.<

Line and position:
 feat. 
     ^

------------------------------------------------------------

Script-Line    : 107
Command        : say
Parameter 1    : > <

Output         : >Olivia Rodrigo|The Kid LAROIfeat. <

Line and position:
 feat. 
     ^
------------------------------------------------------------

But even my attempt with GotoChar 1 doesn't help to get the full, untrimmed content of JOINPHRASE. You can see the missing space for feat. if you look at the result in script line #106

(BTW I have no idea, why the script jumps from #392 to #105 and returns later after #107 back to #397 json_foreach_end after the IfNot-EndIf)

What can I do get this result:
Olivia Rodrigo|The Kid LAROI feat.
without manually adding leading or trailing spaces to the mentioned JOINPHRASE?

You can only use the json_... functions to navigate when in JSON mode, so no gotochar for you there.

See How to Say to output without trimming whitespace? for a similar topic which describes the trimming of whitespace you're observing.

1 Like

Do we have any chance that we can use such results (at least for JSON) untouched?

According to the help you alreay offer the command

Unspace - Removes leading and trailing spaces from the current line

for those who want to trim such content.

It would be great if the content would be left untouched otherwise.

That made me feel very insecure.

Could you please mark all the non available commands for JSON in the help? Or the other way around if it helps...

Or would it be easier to mark such commands in the debug output with something like "not possible in JSON"? Currently, I don't see any hint that GotoChar (for example) doesn't work at all.

I think I spoke too early, gotochar is also working in JSON mode, so no need to feel insecure.

Yes, but many rely on the current behavior and changing it now would mean more support overhead. As you can imagine, answering questions regarding tag sources is mostly my task here (dano and pone have been a huge help there in the past) and it's always a huge time sink, especially without minimal examples.

I'll think about a way to turn on trimming whitespace on output. I could imagine a dedicated function Trim "off|on" which would turn the auto-trimming off and on respectively.

Are you experimenting just out of interest or do you want to develop (and maintain) an alternative MusicBrainz tag source?

1 Like

I try to create a JSON MusicBrainz web source script. If it also work for compilations like "Bravo Hits" - where I struggle currently - I would share it here. I'm pretty sure that a JSON version of MusicBrainz#Artist - Album.src would be more stable and easier to maintain then the XML or even web site parsing with his dozens of regular expressions.

That would be really useful! :christmas_tree: :gift: :santa:

1 Like

I've added Trim "off|on" to this preliminary version for you to try.

https://download.mp3tag.de/support/BEC24A86-ECE4-4F65-BC75-A439E84B7B86-3-11-6-2/mp3tagv311f2setup.exe

Trim "off" disables the automatic trimming of whitespace on all the Say... commands, Trim "on" re-enables it (the default).

2 Likes

Thanks for this! Will give it a test.

And in case you're wondering @LyricsLover, the previous solution has been working fine, but this new trim feature will be a much less hacky approach.

1 Like

I had to update the preliminary version, so if you got v3.11f1 please make sure to update to v3.11f2 from the link above.

2 Likes

v3.11f2 works great in my first attempts. :smiling_face_with_three_hearts:

Thank you very much! :clap:
It makes this case a lot easier.

1 Like

trim is confirmed as working as intended here too.

There is an issue I'd consider similar when it comes to writing multiple tags of the same name for a single track (post).

1 Like

This is now officially part of Mp3tag v3.11h.