Removing instances of "\n" newline from JSON

TL;DR When using KM’s Search and Replace function, how can I can find specific instances of the text \n (backslash n), while excluding any line breaks that were created by pressing Enter?


Details:

I’ve been attempting to pull an RSS feed and parse its JSON using JSONValue, but had been getting blank responses in KM. Through trial and error I was able to figure out that the presence of \n newline breaks in the JSON was defeating KM’s ability to parse it. When I manually remove every occurrence of \n in BBEdit and then paste the RSS into KM, the JSONValue request returns a result as it should.

So I’m attempting to add an additional Search and Replace step to my KM macro, which will ideally remove each occurrence of \n with <br> before I parse the JSON.

However, when I try to replace all appearances of “\n” , KM (rightly) also replaced all standard line breaks in the file (the type you’d get by pressing Enter).

I also tried searching for \\n, thinking maybe it needed to be escaped, but nothing was found.

I could be wrong, but I suspect that the problem is caused by the S&R action interpreting \n as a newline in your source variable, so it can’t see the difference. One possible way to fix that is to modify your source string prior to the S&R action so that each occurrence of \n is replaced with \\n. To do this, you can’t use an S&R action, but you could use a shell script such as sed.

Sorry for making you do some work to test this.

2 Likes

That did it — thank you for the suggestion, @DocOck !

pbpaste | sed 's|\\n|<br>|g' | pbcopy

Another discovery that I made in the process of testing sed:

I noticed by accident that if I pasted the RSS feed text directly into the variable using the Preferences: Variables window, my JSONValue call was working even with the presence of \n. Only when I imported the RSS feed into the variable using Set Variable to Text did the problem occur.

Turns out that when the variable was saved using Set Variable to Text, KM was automatically converting all instances of \n to actual line breaks, and that was apparently what was breaking things.

Now instead I am calling the RSS feed using Get URL, immediately running a few search and replace functions via sed, then saving the resulting RSS to the variable. Success!

The Set Variable to Text action uses a token Text Fields which, by default, interprets \ characters like \n, but you can configure it in the gear menu to interpret only tokens or nothing at all.

4 Likes

Did you mean to type the following, or have I failed to understand? (You were not referring to some hidden token called Text Fields, I think..?)

The Set Variable to Text action uses token Text Fields which, by default, interpret \ characters like \n

Demo of Peter's "don't process the Tokens" suggestion:

JSON newline demo.kmmacros (4.5 KB)

1 Like

I was referring to the token text field as documented in the Text Fields link, not a specific token. Text Fields which accept tokens often (but not always) have a configuration for the action that controls processing in that field.

Thanks, so that would be equivalent to a “text token field” in:

Almost every field in Keyboard Maestro actions is either a numeric field or a text token field.

(I know it can be difficult and even frustrating to predict which wording will confuse somebody.)