Formatt JSON into a Tabbed text

guille
Kilo Expert

Hello Everyone,

I have a requirement where I get a JSON from an xyz source, I need to convert that JSON data into a tabbed / Table like text and insert into a Description field, I've been trying the following:

var JSONString =   current.variables.firewall_change_result;

      var JSONObject = JSON.parse(JSONString);

      var row = JSONObject[0]["rows"][0];

      var cols = JSONObject[0]["rows"][0].length;

      var tabRow = JSONObject[0]["rows"].length;      

      var TAB = "\t\t";

      var strHeader = "Action" + TAB + "Source Address (CIDR Format)" + TAB + "Destination Address(CIDR Format)" + TAB + "Port Number(TCP, UDP)" + TAB + "Description" + "\n";

      var formattedText = "";

      //alert(JSONObject[0]["rows"][0][0].value);// Access Object data

      var i = 0;

      for (i = 0; i < tabRow; i++) {

              for (j = 0; j < cols; j++) {

                      formattedText =     formattedText + JSONObject[0]["rows"][i][j].value + TAB;

                              //gs.print(JSONObject[0]["rows"][j][i].value);

              }

              formattedText = formattedText + "\n" ;

      }

The formatted Text is being placed in description field like this:

find_real_file.png

Unfortunately after hours of try and test I couldn't make it to show it well placed like a table(like those cash machines that prints receipts ), as notices the text is all screwed, I'm looking for suggestions or different approach to take this JSON and place it into description field.

By the way this is the JSON string: I get the nodes that contains row and value, try with a Json Parser or viewer and you will see the content, Thanks!

[
  1. {
    • "hdr":"",
    • "forElement":"",
    • "preFields":[],
    • "rows":[
      1. [
        1. {
          • "field":"column_action",
          • "label":"Action",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"Allow",
          • "display":"Allow",
          • "relatedTable":"",
          • "disabled":false
          },
        2. {
          • "field":"column_source",
          • "label":"Source Address",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"0.0.0.5",
          • "display":"0.0.0.5",
          • "relatedTable":"",
          • "disabled":false
          },
        3. {
          • "field":"column_destination",
          • "label":"Destination Address",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"0.0.0.86",
          • "display":"0.0.0.86",
          • "relatedTable":"",
          • "disabled":false
          },
        4. {
          • "field":"column_port",
          • "label":"Port Number",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"789",
          • "display":"789",
          • "relatedTable":"",
          • "disabled":false
          },
        5. {
          • "field":"column_description",
          • "label":"Description",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"Test",
          • "display":"Test",
          • "relatedTable":"",
          • "disabled":false
          }
        ],
      2. [
        1. {
          • "field":"column_action",
          • "label":"Action",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"Deny",
          • "display":"Deny",
          • "relatedTable":"",
          • "disabled":false
          },
        2. {
          • "field":"column_source",
          • "label":"Source Address",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"www.google.com",
          • "display":"www.google.com",
          • "relatedTable":"",
          • "disabled":false
          },
        3. {
          • "field":"column_destination",
          • "label":"Destination Address",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"192.168.0.26",
          • "display":"192.168.0.26",
          • "relatedTable":"",
          • "disabled":false
          },
        4. {
          • "field":"column_port",
          • "label":"Port Number",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"441",
          • "display":"441",
          • "relatedTable":"",
          • "disabled":false
          },
        5. {
          • "field":"column_description",
          • "label":"Description",
          • "mandatory":"",
          • "type":"text",
          • "giveFocus":"",
          • "reference":"",
          • "choiceOptions":null,
          • "refQual":"",
          • "onChangeFunc":"",
          • "cellCSS":"",
          • "labelCSS":"",
          • "show":"always",
          • "imageSrc":"",
          • "value":"No access google",
          • "display":"No access google",
          • "relatedTable":"",
          • "disabled":false
          }
        ]
      ]
    }
]

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

This won't work with a string field like Description. You can get it to work using an HTML field. Try creating a new HTML field and place it on the form using a table. If you don't want people editing, consider making it read-only.



http://wiki.servicenow.com/index.php?title=Creating_New_Fields


View solution in original post

1 REPLY 1

Chuck Tomasi
Tera Patron

This won't work with a string field like Description. You can get it to work using an HTML field. Try creating a new HTML field and place it on the form using a table. If you don't want people editing, consider making it read-only.



http://wiki.servicenow.com/index.php?title=Creating_New_Fields