The CreatorCon Call for Content is officially open! Get started here.

Hi All, spaces and dots are not taking in request body in scripted rest api

Venkatesh098765
Tera Contributor

Hi All, in one of my json payload there is a field with the value as "test application", while i am trying to retrieve the data i am only getting the first name that is only test from this value test application. However i am able to create a record in my table but the problem is the record gets created with the name a only test not test application. How to avoid these spaces issues? Help me here

12 REPLIES 12

Pratik Malviya
Tera Guru

Hi @Venkatesh098765 ,

 

You should use 

JSON.stringify(JSON.parse(body)

//body is your object variable.
Example:
var body = ' { "name": "test", "description": "test json", "website": "domain.com" } ';
JSON.stringify(JSON.parse(body)

Result:{"name":"test","description":"test json","website":"domain.com"}
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

Hi Pratik,

 

Thanks for the response.Like I am using this way:

 var reqbody = request.body.dataString;
 var str = JSON.stringify(JSON.parse(reqbody));
and then...
  var app_id = str['application_id'].toString();
    var grp_id = str['group_id'].toString();
and then i am gliding my tbale to insert the records in my table .also this is payload i am using
{
'application_id':'12',
'application_name':'App name3',
'group_id':'123',
'group_name':'group name3',
'typrofoperation':'group.application_assignment.add'
}
here when ever i am trying to get the data from this payload ----application_name':'App name3', there is a space in between App name 3 so it was taking only till App after that it was not taking. Not only in application_name, in all the other fields also it was behaving.in the last field 'typrofoperation':'group.application_assignment.add' here also in the value it was taking till group after that it was not taking any data.

Hi @Venkatesh098765 ,

Convert it to string and try to print 

 

var reqbody = request.body.dataString;
var parsedBody = JSON.parse(reqbody);
var app_id = parsedBody.application_id.toString();
var app_name = parsedBody.application_name.toString();
var grp_id = parsedBody.group_id.toString();
var grp_name = parsedBody.group_name.toString();
var typrofoperation = parsedBody.typrofoperation.toString();

Below i tried in PDI BG its working

var reqbody = {
"application_id": "12",
"application_name": "App name3",
"group_id": "123",
"group_name": "group name3",
"typrofoperation": "group.application_assignment.add"
};
var parsedBody = reqbody;
var app_id = reqbody.application_name.toString();
gs.log("app_id: " + app_id);

AnandKumarP_0-1707722921339.png

 

 

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Hi Anand,

 

Thanks for the response. When I am trying this and trying to hit the api and print the data an error was frequently occuring seems like OOB error.please find the error here

Venkatesh098765_0-1707725369786.png