How to bind JSON to dmx-text?

Hi,

I want to output a JSON object in a <code> block. This is not working:

<code dmx-text="JSON.stringify(myForm.data.api.data)"></code> 
<!-- no output -->
<code dmx-text="myForm.data.api.data"></code> 
<!-- outputs [object Object] -->

Any ideas? Thanks!

You can easily replicate this on a project of yours:

<code dmx-text="{}"></code> 
<!-- outputs [object Object], instead of {} -->

Here’s a custom formatter that should work:

Create custom_formatters.js in your js folder

dmx.Formatter('array', 'jsonStringify', function (obj) {

    return JSON.stringify(obj);

});

and reference it:

<script src="js/custom_formatters.js" defer=""></script>

Then you can use:

<code dmx-text="myForm.data.api.data.jsonStringify()"></code>
1 Like

Hi,

I’m getting this warning:

Formatter jsonStringigy in expression [myForm.data.api.data.jsonStringigy()] doesn't exist for type object parser.js:798

I have also tried replacing ‘array’ (on dmx.Formatter) with ‘object’ and ‘string’ - no luck

You have a typo… use jsonStringify instead of jsonStringigy

Oh, my bad!

It’s working now, thanks a lot for your help!

dmx.Formatter('object', 'jsonStringify', function (obj) {
    return JSON.stringify(obj);
});

(had to replace ‘array’ with ‘object’)

1 Like

Ya, it depends on what you pass to it. You can add two versions of the formatter, one for array and one for object.