Preserve Text Formatting in JSON Parameters

Like many others, I now encode script parameters and script results in JSON, and am very happy doing so. However, one shortfall I've found is when trying to pass formatted text (Bold, Size etc) between scripts. The JSON encoding strips all this out, which I guess might be good in some instances, but in others I'd like to preserve it.
Does anyone have a nice way of doing this?...

Don't know if it is nice but you can always pass the formating as an element of the JSON which you apply with a CF at the end of the pipe...

2 Likes

Yeah, that'd work if it was one style for all the text, but I'm dealing with a big blob of text with lots of different formatting throughout. I'm working on a plan now though - I'll report back!...

Yeah my solution was a bit narrow... looking forward to yours! Tell me it's not going to be a giant string in Base64!

Interested in a plugin function?

We already have Text.Styles function in MBS FileMaker Plugin to convert styled text to JSON and we could develop the reversal function.

Email me if you need it...

1 Like

Given the amount of data you're talking about, this could be a case where you don't want to pass around all that data but rather create a work queue table and set that data there and just pass around the id to the queue record.

If you don't care to show that formatted data again in FM (or you can use a webviewer to show the data) then you can do a GetAsCSS() on the text which would make it mostly json-safe.

3 Likes

Hello @JamesG

One approach that has worked:

Start by using the GetAsCSS function to obtain the text in a format which breaks out the various formatting properties that FMP text may contain, and write something which can pull apart the various properties, so that they can be reconstituted at a later time.

I believe that the GetAsSVG function would work equally well, if not better.

Custom Functions for this:

I once wrote a set of custom functions that use the above concept to translate FMP text content with arbitrary formatting into JSON, and then back again to the formatted text. This was to help out someone who wanted to pass text with formatting to an outside system, and then later retrieve the text, including the formatting.

The functions require the use of a plug-in such as: MBS, BaseElements, bBox.

If you'd like to check them out, they are in this archive:

RichTextToJSON_201809031220.fmp12.zip (112.6 KB)

Sans Plug-in:

I understand that many developers would prefer to avoid introducing a plug-in unless truly necessary. I imagine that the above could be implemented without the use of a plug-in. I just have not tried, and can not venture a good guess as to what performance would be like.

EDIT: I just saw @WimDecorte's post, and I have to say that I prefer the idea he proposed about creating a work queue table. If that's possible for you, that would get my vote.

Sincerely & HTH,

-steve

2 Likes

Thanks all! A few good options there - I'll have a think and come back to you with what I find...

Maybe my sample file from here could help:

Pass the text with GetAsCSS(yourText) and convert it back using two CFs: CSStoFMText ( HTMLencoded2Text ( CSSText ) )

FileMaker Custom Functions | Displaying Function HTMLencoded2Text (HTMLencoded2Text)

CSStoFMText was here: https://www.briandunning.com/cf/924 but seems to have disappeared meanwhile.

3 Likes

Unfortunately, formatting texts is not the only thing you will lose.
You will lose 5600 characters, some of which are important like the row and column separators.
You will lose the use of MD5 because it will become conditional on the absence of certain characters in your data.
You will lose the ability to simply (and reliably) exchange data between FileMaker files in a different format (US, French Canadian French) with data including date, numbers or timestamps.

If you use JSON, you will have to manage the date, number and timestamp conversion yourself. Whereas without JSON and by following a few rules FileMaker knows very well how to do it.

If you're using FileMaker lists, dictionaries, or any other technique that uses concatenation, don't give up on it too quickly.

If you understand French, or not, download the following documents to this address.

https://drive.google.com/drive/folders/1F-tewhz4tNFcNuQR1LOAHSurkj6n9PIW

or here

Presentation

In this presentation that I made in Montréal and Poitiers in 2019, I show that using JSON to pass parameters between scripts in FileMaker is the worst technique to use. Especially if you want FileMaker (different origin) files to communicate with each other.

I show that we can pass in FileMaker lists, dictionaries of multidimensional tables separately or simultaneously.

The files are in French but I think the examples are clear.

If you have question i will try to answer even if my english is ...

4 Likes

Excellent and thoroughly documented research! Merci beaucoup, @fragonlesec!

thank you
Were you present in Poitier?

Unfortunately not. I didn’t attend a FM conference in years.

You could go "old school", and use one or more global fields for passing the formatted text portion(s).

"old school" is also good for retaining container metadata...

1 Like

The only way to reliably maintain 100% fidelity is to save the field value to a variable or field and pass a reference to the variable.

However, I recently fixed up an old Custom Function by Christopher Gauntt that converts CSS text returned from GetAsCSS() back to formatted text. I vaguely recall it having some gotchas so be sure to test thoroughly with various styles before using. But it should work in 90% of cases, or maybe more than that.

EDIT 2020-02-16: @steve_ssh fixed a bug in the CF so it handles Sub/Superscript properly now. Please re-download if you downloaded before. Thanks dude.

CSSToFMText ( text )
CSSToFMText.txt (6.1 KB)

4 Likes

Thanks so much for this! Works a treat! Preserves carriage returns, Bold style, text Size and more! Really useful CF, definitely one for the internal library. Thanks again

2 Likes