- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2018 09:21 AM
Hi,
How do I build a JSON request for a Webservice call to be in this format?
{
"Address_Billing":
{
"Description": "{DESCRIPTION}",
"CompanyName": "{COMPANYNAME}",
"Name_First": "{NAME_FIRST}",
"Name_Last": "{NAME_LAST}",
"StreetAddress_Line1": "{STREET_ADDRESS}",
"StreetAddress_Line2": "{STREET_ADDRESS_SECONDARY}",
"City": "{CITYNAME}",
"StateOrProvince": "{STATEABBREVIATIONCODE}",
"PostalCode": "{ZIPCODE}",
"Country": "US",
"Email": "{EMAILADDRESS}",
"Telephone": "{PHONENUMBER}"
},
"Address_Shipping":
{
"Description": "{DESCRIPTION}",
"CompanyName": "{COMPANYNAME}",
"Name_First": "{NAME_FIRST}",
"Name_Last": "{NAME_LAST}",
"StreetAddress_Line1": "{STREET_ADDRESS}",
"StreetAddress_Line2": "{STREET_ADDRESS_SECONDARY}",
"City": "{CITYNAME}",
"StateOrProvince": "{STATEABBREVIATIONCODE}",
"PostalCode": "{ZIPCODE}",
"Country": "US",
"Email": "{EMAILADDRESS}",
"Telephone": "{PHONENUMBER}"
},
"ItemsRequested":[
{
"ProductID": "{PRODUCTID}",
"Quantity": "{QUANTITYREQUESTED}"
}
],
"Comments": "{COMMENTS}",
"PONumber": "{YOUR_PO_NUMBER}",
}
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2018 12:12 PM
400 means that the target system didn't like your request. I made a few modifications in the below script. If the below doesn't work you will need to work with the owners of the target system to see what additional logging within their system is available to help you understand what's wrong with your request.
Summary of changes I made:
- Added request header Content-Type to tell the target system that you are sending Json
- Renamed request to requestBody so its easier to read
- Renamed request1 to restMessage so its easier to read
- removed the gs.print() statement I had in previously
I would also suggest you look at the ServiceNow logs to see if there are any additional errors from your logging of the resposeBody
var restMessage = new sn_ws.RESTMessageV2('Order Request', 'Post');
restMessage.setAuthentication("basic", "My Auth");
restMessage.setRequestHeader("Accept","application/json");
restMessage.setRequestHeader("Content-Type","application/json")
var requestBody = {}
var addressBilling = {};
var addressShipping = {};
var itemsRequested = [];
addressBilling.Description = "A";
addressBilling.CompanyName = "B";
addressBilling.Name_First = "C";
addressBilling.Name_Last = "D";
addressBilling.StreetAddress_Line1 = "E";
addressBilling.StreetAddress_Line2 = "F";
addressBilling.City = "G";
addressBilling.StateOrProvince = "FL";
addressBilling.PostalCode = "20173";
addressBilling.Country = "US";
addressBilling.Email = "abc@abc.com";
addressBilling.Telephone = "1234567890";
addressShipping.Description = "A";
addressShipping.CompanyName = "B";
addressShipping.Name_First = "C";
addressShipping.Name_Last = "D";
addressShipping.StreetAddress_Line1 = "E";
addressShipping.StreetAddress_Line2 = "F";
addressShipping.City = "G";
addressShipping.StateOrProvince = "FL";
addressShipping.PostalCode = "20173";
addressShipping.Country = "US";
addressShipping.Email = "abc@abc.com";
addressShipping.Telephone = "1234567890";
var itemRequested = {};
itemRequested.ProductID = "844152"
itemRequested.Quantity = "1";
itemRequested.AllowBackOrder="false";
itemsRequested.push(itemRequested);
requestBody.Address_Billing = addressBilling;
requestBody.Address_Shipping = addressShipping;
requestBody.ItemsRequested = itemsRequested;
requestBody.Comments = "Comments";
requestBody.PONumber = "PO Number";
requestBody.PromotionCode = "";
//Encode as JSON
requestBody = new global.JSON().encode(requestBody);
restMessage.setRequestBody(requestBody);
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(" responsebody "+responseBody);
gs.log(" httpStatus "+httpStatus);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 03:56 AM
It worked it, but i changed this line:
requestBody = new global.JSON().encode(requestBody);
for this one:
requestBody = JSON.stringify(requestBody);
I apply this solution because i read on API JSON doc, that encode method is deprecated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2020 07:49 PM
Hello Michael,
I am trying this script to get a proper JSON, but since the webservice call is nested array I am not able to achieve it as per your method you mentioned.
webservice call format:
{
"customer": [
{
"contract-id": "1234567899",
"customer-name": "CUST1",
"customer-description": "CUST1",
"customeronboarding": [
{
"service-id": "1",
"region-id": "FCI_MCCP_MUC",
"customer-ip-prefix-range": "192.168.101.0/23"
}
]
}
]
}
*********************************************
The JSON I have tried
var requestJSON = {};
var customers = {};
var testcustomer = [];
var onboardings = {};
var testonboarding = [];
customers['contract_id'] = "124656";
customers['customer-name'] = 'CUST1';
customers['customer-description'] = 'CUST1';
testcustomer.push(customers);
requestJSON.testcustomer = testcustomer;
onboardings['service-id'] = "1";
onboardings['region-id'] = "FCI_MCCP_MUC";
onboardings['customer-ip-prefix-range'] = "192.168.101.0/23";
testonboarding.push(onboardings);
requestJSON.testonboarding = testonboarding;
gs.info('***Request Body ' + JSON.stringify(requestJSON));
*****************************************************
The output that I am getting for the above code
{
"fcicommonfcicustomer":[
{
"contract_id":"124656",
"customer-name":"CUST1",
"customer-description":"CUST1"
}
],
"fcicustonboardingcfscustomeronboarding":[
{
"service-id":"1",
"region-id":"FCI_MCCP_MUC",
"customer-ip-prefix-range":"192.168.101.0/23"
}
]
}
********************************
can you please let me know where I am going wrong?