JSON convert to string in a specific format

Anubhav24
Mega Sage
Mega Sage

Hi All,

 

I need to update a text variable on a RITM form with data in below format:

Name : xyz

Country : qwer

Manager : qwer

Director : asdf

Email : abc@xyz.com, qwe@asd.com,rty@uio.com and so on there are 15-20 fields

 

The values will be fetched from a table , what I have done is I called a script include a built a JSON to be returned to this form and update the value.

 

The problem I am facing is after stringifying the JSON , it is returned in a format that is a little different , it will not have "Enter" after every value it will be separated by comma and it will also have curly braces for empty values and square brackets for arrays for commas when I tried replacing them nextline(\n) character using regular expression it also created nextline(\n) characters for email ids which are separated by commas which is not wanted.

 

Can some one help me build regular expression for all these characters OR suggest me some API for this specific purpose or if there is a better approach to this.

 

Thanks in advance.

1 ACCEPTED SOLUTION

ChiranjeeviR
Kilo Sage

Hi @Anubhav24,

 

You're on the right track by fetching values via a Script Include and populating them into a RITM form field. The issue you're facing is common when dealing with JSON stringification—it isn't formatted the way you'd like for a readable, multiline text field.

Let’s say you have an object like this:

Name : xyz
Country : qwer
Manager : qwer
Director : asdf
Email : abc@xyz.com, qwe@asd.com, rty@uio.com
...

 

But JSON.stringify() gives you:

 

{"Name":"xyz","Country":"qwer","Email":["abc@xyz.com","qwe@asd.com"]}

I would recommend Instead of stringifying the JSON and then trying to parse it again on the client side, convert the JSON into a formatted string in the Script Include (server-side) before returning it. That gives you full control and removes the need for regex.

 

Example: Script Include (Server-side)

Let’s say you have an object like this:

var data = {
  "Name": "xyz",
  "Country": "qwer",
  "Manager": "qwer",
  "Director": "asdf",
  "Email": ["abc@xyz.com", "qwe@asd.com", "rty@uio.com"]
};

 

Format it like this in your Script Include:

var formatted = '';
for (var key in data) {
  if (data.hasOwnProperty(key)) {
    var value = data[key];
    if (Array.isArray(value)) {
      value = value.join(', ');
    }
    if (value == null || value == undefined || value === '') {
      continue; // skip empty fields
    }
    formatted += key + ' : ' + value + '\n';
  }
}
return formatted;

This returns a nicely formatted string to your client script:

 

Name : xyz
Country : qwer
Manager : qwer
Director : asdf
Email : abc@xyz.com, qwe@asd.com, rty@uio.com

 

Thanks and Regards,

Chiranjeevi R

Please mark as Correct Answer/Helpful, if applicable.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

View solution in original post

2 REPLIES 2

Muhammad Salar
Giga Sage

Hello,
Show payload and tell what you want to remove

 

ChiranjeeviR
Kilo Sage

Hi @Anubhav24,

 

You're on the right track by fetching values via a Script Include and populating them into a RITM form field. The issue you're facing is common when dealing with JSON stringification—it isn't formatted the way you'd like for a readable, multiline text field.

Let’s say you have an object like this:

Name : xyz
Country : qwer
Manager : qwer
Director : asdf
Email : abc@xyz.com, qwe@asd.com, rty@uio.com
...

 

But JSON.stringify() gives you:

 

{"Name":"xyz","Country":"qwer","Email":["abc@xyz.com","qwe@asd.com"]}

I would recommend Instead of stringifying the JSON and then trying to parse it again on the client side, convert the JSON into a formatted string in the Script Include (server-side) before returning it. That gives you full control and removes the need for regex.

 

Example: Script Include (Server-side)

Let’s say you have an object like this:

var data = {
  "Name": "xyz",
  "Country": "qwer",
  "Manager": "qwer",
  "Director": "asdf",
  "Email": ["abc@xyz.com", "qwe@asd.com", "rty@uio.com"]
};

 

Format it like this in your Script Include:

var formatted = '';
for (var key in data) {
  if (data.hasOwnProperty(key)) {
    var value = data[key];
    if (Array.isArray(value)) {
      value = value.join(', ');
    }
    if (value == null || value == undefined || value === '') {
      continue; // skip empty fields
    }
    formatted += key + ' : ' + value + '\n';
  }
}
return formatted;

This returns a nicely formatted string to your client script:

 

Name : xyz
Country : qwer
Manager : qwer
Director : asdf
Email : abc@xyz.com, qwe@asd.com, rty@uio.com

 

Thanks and Regards,

Chiranjeevi R

Please mark as Correct Answer/Helpful, if applicable.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.