Using JSON.stringify but SSJS variant in XPages

1.7k views Asked by At

for an application I am building an administration panel where a power user should be able to check the JSON structure of a selected object.

I would like to display the JSON object in a computed text field but display/format it nicely so it is better human readable, something similar as in pretty print.

Is there any function I could use in SSJS that results in something similar so I can use display json nicely in computed text / editable fields?

3

There are 3 answers

3
Eric McCormick On

As Knut points out, you can certainly add json2.js to XPages; I've previously used an implementation as Marky Roden's post outlines. This is probably the "safest" way of doing so, from the SSJS side of things.

It does ignore the included fromJson and toJson SSJS methods provided out of the box in XPages. While imperfect, they are functional, especially with the inclusion of Tommy Valand's fix snippet. Be advised, using Tommy's fix does wrap responses to ensure a proper JS object can be parsed by shoving an Array into an object with a values property for the array; so no direct pulling of an Array only.

Additionally, I believe it would be useful to point out that a bean, providing a convenience method or two as wrappers to use either the com.ibm.commons.util.io.json methods to abstract the conversion method, or switching in something like Google GSON, might be more powerful and unified, based on your style of development.

1
Knut Herrmann On

Use stringify's third parameter "space":

JSON.stringify(yourObject, null, '  ');

space
A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.

As XPages doesn't support JSON.stringify yet you can include JSON's definition as SSJS resource and use it.

2
Patrick Kwinten On

Knut, Eric, I came so far myself already.

function prettyPrint(id) {
    var ugly = dojo.byId(id).value;
    var obj = $.parseJSON( "[" + ugly + "]" );
    var pretty = JSON.stringify(obj, undefined, 4);    
    dojo.byId(id).innerHTML = pretty;
}

and I call it e.g.

var name = x$('#{id:input-currentObjectCollectionFiltered}').attr("name");
prettyPrint(name);

I tried to make use the x$ function but was not able to make the ID dynamic there e.g.

var ugly = x$('#{id:" + id + "}').val();

not sure why. would be nicer if I just would call prettyPrint('input-currentObjectCollectionFiltered'); and the function would figure it out.

Instead of dojo.byId(id).value I tried:

var ugly=$("#" + id).val();

but things returns and undefined object: I thought jquery would be smarter to work with dynamic id's.

anyway stringify works just fine.