Need Help to get value from JSON

SandeepKSingh
Kilo Sage

Hi Team,

I have a JSON :-

{
"orderId": "ORD123456",
"customer": {
"customerId": "CUST789",
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"contactDetails": {
"email": "johndoe@example.com",
"phone": "+1-555-555-5555"
}
},
"orderItems": [
{
"itemId": "ITEM001",
"description": "Laptop",
"quantity": 1,
"price": 1200.00,
"attributes": {
"color": "Silver",
"warranty": "2 years"
}
},
{
"itemId": "ITEM002",
"description": "Wireless Mouse",
"quantity": 2,
"price": 25.00,
"attributes": {
"color": "Black",
"warranty": "1 year"
}
}
],
"orderStatus": "Shipped",
"shippingDetails": {
"carrier": "UPS",
"trackingNumber": "1Z999AA10123456784",
"expectedDelivery": "2024-08-25"
}
}
I need the "Shipping Carrier" and "Tracking Number" . How to achieve this??

2 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hi RGGuide,

 

----------- Please Use the below Script ------------------------

 

// Your JSON object
var orderData = {
"orderId": "ORD123456",
"customer": {
"customerId": "CUST789",
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"contactDetails": {
"email": "johndoe@example.com",
"phone": "+1-555-555-5555"
}
},
"orderItems": [
{
"itemId": "ITEM001",
"description": "Laptop",
"quantity": 1,
"price": 1200.00,
"attributes": {
"color": "Silver",
"warranty": "2 years"
}
},
{
"itemId": "ITEM002",
"description": "Wireless Mouse",
"quantity": 2,
"price": 25.00,
"attributes": {
"color": "Black",
"warranty": "1 year"
}
}
],
"orderStatus": "Shipped",
"shippingDetails": {
"carrier": "UPS",
"trackingNumber": "1Z999AA10123456784",
"expectedDelivery": "2024-08-25"
}
};

// Extract the "Shipping Carrier" and "Tracking Number"
var shippingCarrier = orderData.shippingDetails.carrier;
var trackingNumber = orderData.shippingDetails.trackingNumber;

// Output the values
gs.print("Shipping Carrier: " + shippingCarrier);
gs.print("Tracking Number: " + trackingNumber);


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

Can you try method 2 :-

 

var json = {
"orderId": "ORD123456",
"customer": {
"customerId": "CUST789",
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"contactDetails": {
"email": "johndoe@example.com",
"phone": "+1-555-555-5555"
}
},
"orderItems": [
{
"itemId": "ITEM001",
"description": "Laptop",
"quantity": 1,
"price": 1200.00,
"attributes": {
"color": "Silver",
"warranty": "2 years"
}
},
{
"itemId": "ITEM002",
"description": "Wireless Mouse",
"quantity": 2,
"price": 25.00,
"attributes": {
"color": "Black",
"warranty": "1 year"
}
}
],
"orderStatus": "Shipped",
"shippingDetails": {
"carrier": "UPS",
"trackingNumber": "1Z999AA10123456784",
"expectedDelivery": "2024-08-25"
}
};

var shippingCarrier = json['shippingDetails']['carrier'];
var trackingNumber = json['shippingDetails']['trackingNumber'];

gs.log("Shipping Carrier: " + shippingCarrier);
gs.log("Tracking Number: " + trackingNumber);


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

Najmuddin Mohd
Mega Sage

Hi @SandeepKSingh ,
Store the JSON in variable.

var obj = ..........(JSON)

To acess,

gs.info(JSON.stringify(obj.shippingDetails.carrier));
gs.info(JSON.stringify(obj.shippingDetails.trackingNumber));
 
NajmuddinMohd_1-1724244279565.png

 

 

 



NajmuddinMohd_0-1724244212333.png



If the above information helps you, Kindly mark it as Helpful and Accept the solution.

Regards,
Najmuddin.

Ravi Gaurav
Giga Sage
Giga Sage

Hi RGGuide,

 

----------- Please Use the below Script ------------------------

 

// Your JSON object
var orderData = {
"orderId": "ORD123456",
"customer": {
"customerId": "CUST789",
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"contactDetails": {
"email": "johndoe@example.com",
"phone": "+1-555-555-5555"
}
},
"orderItems": [
{
"itemId": "ITEM001",
"description": "Laptop",
"quantity": 1,
"price": 1200.00,
"attributes": {
"color": "Silver",
"warranty": "2 years"
}
},
{
"itemId": "ITEM002",
"description": "Wireless Mouse",
"quantity": 2,
"price": 25.00,
"attributes": {
"color": "Black",
"warranty": "1 year"
}
}
],
"orderStatus": "Shipped",
"shippingDetails": {
"carrier": "UPS",
"trackingNumber": "1Z999AA10123456784",
"expectedDelivery": "2024-08-25"
}
};

// Extract the "Shipping Carrier" and "Tracking Number"
var shippingCarrier = orderData.shippingDetails.carrier;
var trackingNumber = orderData.shippingDetails.trackingNumber;

// Output the values
gs.print("Shipping Carrier: " + shippingCarrier);
gs.print("Tracking Number: " + trackingNumber);


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Ravi Gaurav
Giga Sage
Giga Sage

Can you try method 2 :-

 

var json = {
"orderId": "ORD123456",
"customer": {
"customerId": "CUST789",
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"contactDetails": {
"email": "johndoe@example.com",
"phone": "+1-555-555-5555"
}
},
"orderItems": [
{
"itemId": "ITEM001",
"description": "Laptop",
"quantity": 1,
"price": 1200.00,
"attributes": {
"color": "Silver",
"warranty": "2 years"
}
},
{
"itemId": "ITEM002",
"description": "Wireless Mouse",
"quantity": 2,
"price": 25.00,
"attributes": {
"color": "Black",
"warranty": "1 year"
}
}
],
"orderStatus": "Shipped",
"shippingDetails": {
"carrier": "UPS",
"trackingNumber": "1Z999AA10123456784",
"expectedDelivery": "2024-08-25"
}
};

var shippingCarrier = json['shippingDetails']['carrier'];
var trackingNumber = json['shippingDetails']['trackingNumber'];

gs.log("Shipping Carrier: " + shippingCarrier);
gs.log("Tracking Number: " + trackingNumber);


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/