1 2 Previous Next 18 Replies Latest reply on Jul 30, 2015 8:55 AM by beverly

# Substitute function for json

Ok its been a long day so my brain may be just not thinking about this logically but I just can't figure out why..

I want to replace all my " in my text with \" and any \ with \\ . So in the case where myText is aa\bb"cc i need it to be aa\\bb\"cc

so at first I tried this

Substitute ( myText ; ["\\" ; "\\\\"];[ "\"" ; "\\\"" ]  )

and the output was aa\\\bb\\\"cc" not what I was expecting.  It appears the replacement value doesn't use the escapes so after experimenting this worked.

Substitute ( myText ; [ "\"" ; "\"" ];["\\" ; "\\"] )

what I am trying to accomplish is to have myText a value that would be safe to use as a value in a JSON object so if anybody knows a cf or something that world be great.

Thanks

• ###### 1. Re: Substitute function for json

I would have changed the \ first:

Substitute ( mytext ;  ;  )

The Char() function helps keep it straight. Char(92) is the backslash and Char(34) is the double-quote). Using these prevents escaping from the whole calculation (pun).

beverly

• ###### 2. Re: Substitute function for json

Or you could try this:

Substitute ( theText ; [ "\\" ; "\\" & "\\"] ; ["\"" ; "\\" & "\""]  )

The substitute will process the string in sequence taking the output result of first parameter as input for the second one.

So looks like you need to take care of the slashes first, then the escaped quotes.

Do something in "FileMaker" turn \single backslash into \double one.

Do something in \"FileMaker\" turn \\single backslash into \\double one.

• ###### 3. Re: Substitute function for json

And in the original case, the order of replacements is also important :

Substitute ( TextFieldBefore

;

[ Caractere ( 92 ) ; Caractere ( 92 ) & Caractere ( 92 ) ]

;

[ Caractere ( 34 ) ; Caractere ( 92 ) & Caractere ( 34 ) ]

)

• ###### 4. Re: Substitute function for json

Is it just me, or does anyone else *hate* that the square brackets get munged when reply-by-email?!

Here's the calc:

Substitute ( mytext ; [ char(92) ; char(92) & char(92) ] ; [ char(34) ; char(92) & char(34) ] )

Which is what Fred said, too.

• ###### 5. Re: Substitute function for json

testing square brackets (ignore):

\[ start \] end

beverly

• ###### 6. Re: Substitute function for json

so reply-by-email will allow escaped square brackets to be posted, but keeps the backslash...

I guess it's better than blanking out the text between!

• ###### 7. Re: Substitute function for json

But i forgot to translate "Caractere" to "Char" …

• ###### 8. Re: Substitute function for json

Mmh i often use the anti slash to escape some chars like ", for instance with Evaluation function. And with this case here, it is true that to write the escape char itself you have to double it.

However, sounds like i missed something important on your comment below :

Beverly Voth a écrit:

testing square brackets (ignore):

\[ start \] end

beverly

• ###### 9. Re: Substitute function for json

That was a reply by email Fred. If I post  <- this is blank. If I post \[something\] <- this is NOT blank, but also retains the square brackets.

Do NOT use \ and square brackets in your calculations!

Something about JIVE messes them, but only when posting by email. Posts while logged in do not change them.

-- sent from myPhone --

Beverly Voth

--

• ###### 10. Re: Substitute function for json

Ah ok, it was the reason why you didn't post the entire formula and there was a bit of room for my comment .

I even didn't know that one could post here by mail... cool ! And i am now aware about potential problem with, Thanks !

And sorry for misunderstanding (i am doing my best, especially against my frenchitude)

Fred

• ###### 11. Re: Substitute function for json

no worries!

here is another test: \[something] (just the opening square bracket has been 'escaped')

I guess I can post by email and explain that the backslash is only because of Jive!!

beverly

• ###### 12. Re: Substitute function for json

I wonder if the fts or another source has more detail on what exactly happens in the substitute function.  I am still wondering why the first function didn't work.  Looks like some more study is needed something I use quite a bit so getting a result that is unexpected is a bit humbling.

• ###### 13. Re: Substitute function for json

Let (

myText = "This is \"quoted\", this is backslashed \\backslashed\\ text.";

Substitute ( myText ; ["\\" ; "\\\\"];[ "\"" ; "\\\"" ]  )

// myText

)

Result:

This is \"quoted\", this is backslashed \\backslashed\\ text.

Seems to be working here.

• ###### 14. Re: Substitute function for json

ok this is crazy!! just gave it a try and if i use [ Char(92) ; Char(92) & Char(92) ] it turns \ into \\\

1 2 Previous Next