Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Help parsing JSON payload

athavichith
Mega Sage

I need helping parsing out a sample JSON payload. I want to parse the sample payload into the proc_po, proc_po_item tables. 

 

{

"purchaseOrderDetails": [

{

"purchaseOrderNumber": "DELL-88888",

"purchaseOrderDate": "2022-11-21T22:14:52Z",

"purchaseOrderStatus": "Shipped",

"dellOrders": [

{

"orderNumber": "565433333",

"dellPurchaseId": "2009428666666",

"orderStatus": "Shipped",

"statusDateTime": "2022-11-24T20:44:49Z",

"productInfo": [

{

"skuNumber": "210-AZYQ",

"description": "PowerEdge R750xs Server",

"itemQuantity": "2",

"serviceTags": [

"BB7HST5",

"CS7TGT5"

]

}

],

"invoiceNumber": null,

"invoiceDate": null,

"actualShipmentDate": "2022-11-24T20:34:02Z",

"actualDeliveryDate": null,

"estimatedShipmentDate": "2022-12-29T00:00:00Z",

"estimatedDeliveryDate": "2023-01-05T06:00:00Z",

"revisedShipmentDate": "2022-11-25T10:47:20Z",

"revisedDeliveryDate": "2022-11-30T10:47:20Z",

"revisedEstimatedOrDeliveryDateChange": false,

"trackingInformation": [

{

"carrierName": "FEDEX GROUND",

"waybill": "613947999999",

"carrierMileStone": "LABEL CREATED",

"carrierTrackingURL": "https://www.fedex.com/fedextrack/?trknbr=613947999999",

"milestoneStatusChange": false,

"milestoneEventDescription": "Shipment Acknowledged"

},

{

"carrierName": "FEDEX GROUND",

"waybill": "613947999999",

"carrierMileStone": null,

"carrierTrackingURL": "https://www.fedex.com/fedextrack/?trknbr=613947999999",

"milestoneStatusChange": false,

"milestoneEventDescription": null

}

],

"shipToInformation": {

"companyName": "COMPANY INC",

"contactName": "JOHN SMITH",

"contactAddress1": "ADDRESS LINE 1",

"contactAddress2": null,

"city": "HOUSTON",

"stateProvince": "Texas",

"postalCode": "77015-6586",

"country": "United States"

},

"purchaseOrderLines": [

{

"lineNumber": "1",

"buyerPartNumber": "3000134444444.1",

"dellPartNumber": "3000134444444.1",

"description": null,

"unitPrice": 8516.85,

"quantityOrdered": "2",

"lineStatus": "Shipped"

}

]

}

]

}

]

}

1 ACCEPTED SOLUTION

Saurav11
Kilo Patron

Hello,

 

the sample code would look something like below;-

 

if (response.getStatusCode() == 200) {
  var data = JSON.parse(responseBody);
  for (var i = 0; i < data.purchaseOrderDetails.length; i++) {
    var incident = data.result[i];
   var fieldname1 =incident.purchaseOrderNumber
  }
} else {
  gs.error("Failed to retrieve incident data: " + response.getErrorMessage());
}

 

Please refer the below article for the same:-

 

https://www.saaswithservicenow.in/post/how-to-parse-a-json-response-in-servicenow

 

Please mark my answer as correct based on Impact.

View solution in original post

3 REPLIES 3

Saurav11
Kilo Patron

Hello,

 

the sample code would look something like below;-

 

if (response.getStatusCode() == 200) {
  var data = JSON.parse(responseBody);
  for (var i = 0; i < data.purchaseOrderDetails.length; i++) {
    var incident = data.result[i];
   var fieldname1 =incident.purchaseOrderNumber
  }
} else {
  gs.error("Failed to retrieve incident data: " + response.getErrorMessage());
}

 

Please refer the below article for the same:-

 

https://www.saaswithservicenow.in/post/how-to-parse-a-json-response-in-servicenow

 

Please mark my answer as correct based on Impact.

With the payload above, it recognized it as object and not as array. This is what I did and it is now working. 

var parsedData = request.body.data;

// Ensure parsedData has the purchaseOrderDetails property and it's an array
if (!parsedData.hasOwnProperty('purchaseOrderDetails') || !Array.isArray(parsedData.purchaseOrderDetails)) {
throw new Error("purchaseOrderDetails property is not an array");
}

var ordersArray = parsedData.purchaseOrderDetails;

Laurent5
ServiceNow Employee

Hi I am having similar issues parsing content returned by an API.

the content looks as below and I am trying to only return what is after "result" (in italic) with no success (albeit using the approach above. Any pointers welcome. 

response body:

{"output":{"result":"laying down harmonised rules on artificial intelligence","source_documents":[{"page_content":"Text proposed by the Commission Amendment\n3. Where high-risk AI systems are \nsubject to other Union harmonisation \nlegislation which also requires an EU \ndeclaration of conformity, a single EU","metadata":{"page":231,"source":"https://www.europarl.europa.eu/doceo/document/TA-9-2023-0236_EN.pdf"},"type":"Document"},{"page_content":"a regulation of the European Parliament and of the Council on laying down harmonised \nrules on artificial intelligence (Artificial Intelligence Act) and amending certain Union","met

 

My code:

(function execute(inputs, outputs) {

if (inputs.status_code == '200'){
var data = JSON.parse(inputs.response_body);
for (var i = 0; i < data.output.length; i++) {
var result_var = data.result[i];
}
} else {
gs.error("Failed to retrieve message data: " + response.getErrorMessage());
}
// Map the parsed responseBody values to output variables
outputs.llm_response = result_var;

})(inputs, outputs);