Format JSON into a Tabbed text

Community Alums
Not applicable

AbdulKhader_0-1677047291223.png

I have to change this json into tabbed text

My script :

getUser: function(sysid) {
var userID = sysid;
if (userID != '') {

var count = 0;
var arr = [];
var obj = ["sys_script_execution_history", "sys_ui_list", "sys_ui_navigator_history", "sc_cart", "sn_grc_unique_user_usage", "sn_km_mr_st_kb_knowledge", "sys_mass_encryption_job", "sys_email", "ua_app_usage", "sys_ui_bookmark", "sys_user_preference", "text_search", "cxs_relevant_doc", "signature_image", "sys_rollback_context", "wf_workflow_version", "sys_history_line0000", "label_history", "sys_user_presence", "sp_log"];
gs.info(obj);

var gr = new GlideRecord('sys_dictionary');
gr.addEncodedQuery('nameNOT IN' + obj);
gr.addQuery('internal_type=reference^ORinternal_type=glide_list^reference=sys_user');
gr.query();
while (gr.next()) {
try {
var tableName = gr.getDisplayValue('name');
var fieldName = gr.getDisplayValue('element');
var referenceName = gr.getDisplayValue('reference');
var grTab = new GlideRecord(tableName);
grTab.addEncodedQuery(fieldName + '=' + userID);
grTab.query();
if (grTab.next()) {
count = grTab.getRowCount();
arr.push({
"table": tableName,
"field": fieldName,
"count": count
});
}
} catch (e) {}
}
}
return JSON.stringify(arr);
},

3 REPLIES 3

Sai Shravan
Mega Sage

Hi @Community Alums ,

 

To convert the JSON output into tabbed text, you can use a loop to iterate over the array and concatenate the values with tab separators.

getUser: function(sysid) {
  var userID = sysid;
  if (userID != '') {
    var count = 0;
    var arr = [];
    var obj = ["sys_script_execution_history", "sys_ui_list", "sys_ui_navigator_history", "sc_cart", "sn_grc_unique_user_usage", "sn_km_mr_st_kb_knowledge", "sys_mass_encryption_job", "sys_email", "ua_app_usage", "sys_ui_bookmark", "sys_user_preference", "text_search", "cxs_relevant_doc", "signature_image", "sys_rollback_context", "wf_workflow_version", "sys_history_line0000", "label_history", "sys_user_presence", "sp_log"];
    gs.info(obj);
    var gr = new GlideRecord('sys_dictionary');
    gr.addEncodedQuery('nameNOT IN' + obj);
    gr.addQuery('internal_type=reference^ORinternal_type=glide_list^reference=sys_user');
    gr.query();
    while (gr.next()) {
      try {
        var tableName = gr.getDisplayValue('name');
        var fieldName = gr.getDisplayValue('element');
        var referenceName = gr.getDisplayValue('reference');
        var grTab = new GlideRecord(tableName);
        grTab.addEncodedQuery(fieldName + '=' + userID);
        grTab.query();
        if (grTab.next()) {
          count = grTab.getRowCount();
          arr.push({
            "table": tableName,
            "field": fieldName,
            "count": count
          });
        }
      } catch (e) {}
    }
    var result = "Table\tField\tCount\n"; // column headers with tab separators
    for (var i = 0; i < arr.length; i++) {
      var table = arr[i].table;
      var field = arr[i].field;
      var count = arr[i].count;
      result += table + "\t" + field + "\t" + count + "\n"; // row with tab-separated values
    }
    return result; // return the tab-separated text
  }
}

 

This code will output a tab-separated text string that includes the table name, field name, and count for each entry in the original JSON array. Note that the "\t" characters represent tabs, and the "\n" character represents a line break. You can modify the formatting as needed for your specific use case.

 

Regards,
Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Community Alums
Not applicable

AbdulKhader_0-1677048736274.png

Its Working but /n doesn't break into line

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Community Alums ,

 

If you want the result to be in new line, you can use the formatter of the stringify() method of JSON. In your script where you are returning the JSON.stringify(arr), replace that with below line of code, It should work.


JSON.stringify(arr, null, "\t");

 

I hope this helps.

 

Please hit Helpful if this helps and mark the response as Answer if this solves your issue.

 

Regards,

Kamlesh