Convert Object to JSON

I have an object from a server action and wanted to convert it to JSON. I tried JSON.stringify() but it doesn’t seem to be support in the expressions. What is the best way to convert to a JSON string within an expression?

@jamesw u can try
Custom formatter ;

<script>
        dmx.Formatter('array','stringify',  function(val) {
            return JSON.stringify(val);
        });
</script>
1 Like

Thanks! It works, but you have to use 'object' not 'array', since I’ converting an object in my case.

You want to convert to JSON or String?
Responses from server action are JSON already.
And JSON.stringify returns a string for the JSON supplied.

Formatter shared by Serhat should work fine if its the string you are after.

1 Like

As stated “object from a server action and wanted to convert it to JSON” So object to JSON (string obviously, since JSON once converted to an object is just an object, and literal objects are not JSON :wink: ). It’s only for debug/diagnostic purposes to view objects on the client side, like error ‘response’ objects, etc.

And yes, you can press F12 to see messages, but sometimes it’s faster to just show it on the page while developing.

The stringfiy can also be used for array and object. You can update and use it however you want.

I know that. Just saying, in my case, it’s an object, in case anyone else is looking to do something similar. :wink:

Don't think that's correct. A string is not JSON.

Show me where I said a string was JSON? :wink:

here. :man_shrugging:

Yes, JSON is a string, that’s doesn’t mean a string is JSON. I think you’ve misreading that.

Translated: object to JSON (which implies a string

hi @s.alpaslan, where i need put this exactly? and u can show the example of use? tks!!!

i read somes artitles on forum, but all very poor to implementing!

Come on Sid - sort yourself out :rofl: :joy: (#ffs)

1 Like

hi @AlexandreMoreira ,

Reading the following link will actually help you. Also, if you encounter any issues, please write to us with the exact problem, and we will be happy to assist you.