ruast
May 22, 2022, 11:56am
1
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?
bpj
May 22, 2022, 2:51pm
2
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
ruast
May 22, 2022, 2:56pm
3
Thank you so much, just what i needed
1 Like
sid
May 22, 2022, 3:54pm
4
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.
ruast
May 22, 2022, 3:59pm
5
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()
sid
May 22, 2022, 4:44pm
7
json(session.data.stats.where(‘gid’, ‘A100’, ‘==’)[0])
1 Like
sid
May 22, 2022, 4:45pm
8
These two are for server side (server actions). This is for client side.
1 Like