How to convert array item to string

I have an array in a session variable called stats which has the following items.

{gid: "A100", FIB: 100, MVP: 0}
{gid: "A101", FIB: 95, MVP: 0}

I want to return an item at a particular index as a string

The expression below returns an object
[object%20Object]

session.data.stats.where('gid', 'A100', '==')[0] 

I want it to return the following string

{gid: "A100", FIB: 100, MVP: 0}

How do i do that?

You would need a custom formatter

dmx.Formatter('object', 'stringify', function stingfy(val) {
    return JSON.stringify(val);
})

place that in a JS file and include it on your page (or put it between script tags on the page). Then use it like you would any other formatter:

somevalue.stringify()
1 Like

Thank you so much, just what i needed

1 Like

There is also a json() function. Its not in the UI, but part of DMX AppConnect.
It does the same thing as stringify - so no need for a custom formatter anymore for this.

How do i use it ?

i tried

session.data.stats.where('gid', 'A100', '==')[0].JSON()

and

session.data.stats.where('gid', 'A100', '==')[0].json()

but it did not work

I think the json functions are .toJSON() and .parseJSON()

json(session.data.stats.where(‘gid’, ‘A100’, ‘==’)[0])

1 Like

These two are for server side (server actions). This is for client side.

1 Like

This worked thanks!