DataFlex / ShipEngine Walkthrough / Parse shipment info w/ known fields
Back to Collection Items
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoHttp
Boolean iSuccess
Variant vJson
Handle hoJson
Variant vResp
Handle hoResp
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJResp
Integer iRespStatusCode
Integer iValue
String sUnit
Integer iLength
Integer iWidth
Integer iHeight
String sDimensionsUnit
Integer iAmount
String sV_Currency
String sV_type
String sText
Integer iStart_index
Integer iEnd_index
Integer iResultValue
String sResultUnit
Integer iResultLength
Integer iResultWidth
Integer iResultHeight
String sDirection
String sResultName
String sResultCompany_name
String sResultAddress_line1
String sResultCity_locality
String sResultState_province
String sResultPostal_code
String sResultAddress_residential_indicator
Integer iLine
String sV_Type
String sScore
String sConfirmation
String sName
String sCompany_name
String sAddress_line1
String sCity_locality
String sState_province
String sPostal_code
String sAddress_residential_indicator
String sShip_fromName
String sShip_fromCompany_name
String sPhone
String sShip_fromAddress_line1
String sShip_fromCity_locality
String sShip_fromState_province
String sShip_fromPostal_code
String sCountry_code
String sShip_fromAddress_residential_indicator
Integer i
Integer iCount_i
String sTemp1
Boolean bTemp1
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "text": "I need to ship a 17lb package that’s 36x12x24in. It’s going to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature please.",
// "shipment": {
// "ship_from": {
// "name": "John Doe",
// "company_name": "Example Corp.",
// "address_line1": "4009 Marathon Blvd",
// "city_locality": "Austin",
// "state_province": "TX",
// "postal_code": "78756",
// "country_code": "US",
// "phone": "512-555-5555"
// }
// }
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "text" "I need to ship a 17lb package that’s 36x12x24in. It’s going to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature please." To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.name" "John Doe" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.company_name" "Example Corp." To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.address_line1" "4009 Marathon Blvd" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.city_locality" "Austin" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.state_province" "TX" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.postal_code" "78756" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.country_code" "US" To iSuccess
Get ComUpdateString Of hoJson "shipment.ship_from.phone" "512-555-5555" To iSuccess
Send ComSetRequestHeader To hoHttp "API-Key" "{{API_KEY}}"
Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
Get pvComObject of hoJson to vJson
Get ComPostJson3 Of hoHttp "https://api.shipengine.com/v1/shipments/recognize" "application/json" vJson To vResp
If (IsComObject(vResp)) Begin
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
Set pvComObject Of hoResp To vResp
End
Get ComLastMethodSuccess Of hoHttp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
If (Not(IsComObjectCreated(hoJResp))) Begin
Send CreateComObject of hoJResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJResp To False
Showln "Response Body:"
Get ComEmit Of hoJResp To sTemp1
Showln sTemp1
Get ComStatusCode Of hoResp To iRespStatusCode
Showln "Response Status Code = " iRespStatusCode
If (iRespStatusCode >= 400) Begin
Showln "Response Header:"
Get ComHeader Of hoResp To sTemp1
Showln sTemp1
Showln "Failed."
Send Destroy of hoResp
Procedure_Return
End
Send Destroy of hoResp
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "score": 0.9604458751176728,
// "shipment": {
// "confirmation": "adult_signature",
// "ship_to": {
// "name": "Amanda Miller",
// "company_name": "Adult Signature",
// "address_line1": "525 Winchester Blvd",
// "city_locality": "San Jose",
// "state_province": "CA",
// "postal_code": "95128",
// "address_residential_indicator": "yes"
// },
// "ship_from": {
// "name": "John Doe",
// "company_name": "Example Corp.",
// "phone": "512-555-5555",
// "address_line1": "4009 Marathon Blvd",
// "city_locality": "Austin",
// "state_province": "TX",
// "postal_code": "78756",
// "country_code": "US",
// "address_residential_indicator": "unknown"
// },
// "packages": [
// {
// "weight": {
// "value": 17,
// "unit": "pound"
// },
// "dimensions": {
// "length": 36,
// "width": 12,
// "height": 24,
// "unit": "inch"
// },
// "insured_value": {
// "amount": 400,
// "currency": "USD"
// }
// }
// ]
// },
// "entities": [
// {
// "type": "weight",
// "score": 0.9805313966503588,
// "text": "17lb",
// "start_index": 17,
// "end_index": 20,
// "result": {
// "value": 17,
// "unit": "pound"
// }
// },
// {
// "type": "dimensions",
// "score": 1,
// "text": "36x12x24in",
// "start_index": 37,
// "end_index": 46,
// "result": {
// "length": 36,
// "width": 12,
// "height": 24,
// "unit": "inch"
// }
// },
// {
// "type": "dimension",
// "score": 0.9805313966503588,
// "text": "24in",
// "start_index": 43,
// "end_index": 46,
// "result": {
// "unit": "inch",
// "value": 24
// }
// },
// {
// "type": "address",
// "score": 0.9686815805970408,
// "text": "to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature",
// "start_index": 60,
// "end_index": 228,
// "result": {
// "direction": "to",
// "name": "Amanda Miller",
// "company_name": "Adult Signature",
// "address_line1": "525 Winchester Blvd",
// "city_locality": "San Jose",
// "state_province": "CA",
// "postal_code": "95128",
// "address_residential_indicator": "yes"
// }
// },
// {
// "type": "person",
// "score": 0.9519646137063122,
// "text": "Amanda Miller",
// "start_index": 63,
// "end_index": 75,
// "result": {
// "value": "Amanda Miller"
// }
// },
// {
// "type": "residential_indicator",
// "score": 0.9519646137063122,
// "text": "house",
// "start_index": 79,
// "end_index": 83,
// "result": {
// "value": "yes"
// }
// },
// {
// "type": "address_line",
// "score": 0.9805313966503588,
// "text": "525 Winchester Blvd",
// "start_index": 88,
// "end_index": 106,
// "result": {
// "line": 1,
// "value": "525 Winchester Blvd"
// }
// },
// {
// "type": "number",
// "score": 0.9805313966503588,
// "text": "525",
// "start_index": 88,
// "end_index": 90,
// "result": {
// "type": "cardinal",
// "value": 525
// }
// },
// {
// "type": "city_locality",
// "score": 0.9805313966503588,
// "text": "San Jose",
// "start_index": 111,
// "end_index": 118,
// "result": {
// "value": "San Jose"
// }
// },
// {
// "type": "state_province",
// "score": 0.9805313966503588,
// "text": "California",
// "start_index": 120,
// "end_index": 129,
// "result": {
// "name": "California",
// "value": "CA"
// }
// },
// {
// "type": "postal_code",
// "score": 0.9519646137063122,
// "text": "95128",
// "start_index": 148,
// "end_index": 152,
// "result": {
// "value": "95128"
// }
// },
// {
// "type": "insurance",
// "score": 0.8530163983409642,
// "text": "insure it for $400",
// "start_index": 180,
// "end_index": 197,
// "result": {
// "value": 400,
// "unit": "USD"
// }
// },
// {
// "type": "insured_value",
// "score": 1,
// "text": "$400",
// "start_index": 194,
// "end_index": 197,
// "result": {
// "unit": "USD",
// "value": 400
// }
// },
// {
// "type": "company",
// "score": 0.9519646137063122,
// "text": "adult signature",
// "start_index": 214,
// "end_index": 228,
// "result": {
// "value": "Adult Signature"
// }
// },
// {
// "type": "delivery_confirmation",
// "score": 0.8530163983409642,
// "text": "adult signature",
// "start_index": 214,
// "end_index": 228,
// "result": {
// "name": "Adult Signature",
// "value": "adult_signature"
// }
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
Get ComStringOf Of hoJResp "score" To sScore
Get ComStringOf Of hoJResp "shipment.confirmation" To sConfirmation
Get ComStringOf Of hoJResp "shipment.ship_to.name" To sName
Get ComStringOf Of hoJResp "shipment.ship_to.company_name" To sCompany_name
Get ComStringOf Of hoJResp "shipment.ship_to.address_line1" To sAddress_line1
Get ComStringOf Of hoJResp "shipment.ship_to.city_locality" To sCity_locality
Get ComStringOf Of hoJResp "shipment.ship_to.state_province" To sState_province
Get ComStringOf Of hoJResp "shipment.ship_to.postal_code" To sPostal_code
Get ComStringOf Of hoJResp "shipment.ship_to.address_residential_indicator" To sAddress_residential_indicator
Get ComStringOf Of hoJResp "shipment.ship_from.name" To sShip_fromName
Get ComStringOf Of hoJResp "shipment.ship_from.company_name" To sShip_fromCompany_name
Get ComStringOf Of hoJResp "shipment.ship_from.phone" To sPhone
Get ComStringOf Of hoJResp "shipment.ship_from.address_line1" To sShip_fromAddress_line1
Get ComStringOf Of hoJResp "shipment.ship_from.city_locality" To sShip_fromCity_locality
Get ComStringOf Of hoJResp "shipment.ship_from.state_province" To sShip_fromState_province
Get ComStringOf Of hoJResp "shipment.ship_from.postal_code" To sShip_fromPostal_code
Get ComStringOf Of hoJResp "shipment.ship_from.country_code" To sCountry_code
Get ComStringOf Of hoJResp "shipment.ship_from.address_residential_indicator" To sShip_fromAddress_residential_indicator
Move 0 To i
Get ComSizeOfArray Of hoJResp "shipment.packages" To iCount_i
While (i < iCount_i)
Set ComI Of hoJResp To i
Get ComIntOf Of hoJResp "shipment.packages[i].weight.value" To iValue
Get ComStringOf Of hoJResp "shipment.packages[i].weight.unit" To sUnit
Get ComIntOf Of hoJResp "shipment.packages[i].dimensions.length" To iLength
Get ComIntOf Of hoJResp "shipment.packages[i].dimensions.width" To iWidth
Get ComIntOf Of hoJResp "shipment.packages[i].dimensions.height" To iHeight
Get ComStringOf Of hoJResp "shipment.packages[i].dimensions.unit" To sDimensionsUnit
Get ComIntOf Of hoJResp "shipment.packages[i].insured_value.amount" To iAmount
Get ComStringOf Of hoJResp "shipment.packages[i].insured_value.currency" To sV_Currency
Move (i + 1) To i
Loop
Move 0 To i
Get ComSizeOfArray Of hoJResp "entities" To iCount_i
While (i < iCount_i)
Set ComI Of hoJResp To i
Get ComStringOf Of hoJResp "entities[i].type" To sV_type
Get ComStringOf Of hoJResp "entities[i].score" To sScore
Get ComStringOf Of hoJResp "entities[i].text" To sText
Get ComIntOf Of hoJResp "entities[i].start_index" To iStart_index
Get ComIntOf Of hoJResp "entities[i].end_index" To iEnd_index
Get ComIntOf Of hoJResp "entities[i].result.value" To iResultValue
Get ComStringOf Of hoJResp "entities[i].result.unit" To sResultUnit
Get ComIntOf Of hoJResp "entities[i].result.length" To iResultLength
Get ComIntOf Of hoJResp "entities[i].result.width" To iResultWidth
Get ComIntOf Of hoJResp "entities[i].result.height" To iResultHeight
Get ComStringOf Of hoJResp "entities[i].result.direction" To sDirection
Get ComStringOf Of hoJResp "entities[i].result.name" To sResultName
Get ComStringOf Of hoJResp "entities[i].result.company_name" To sResultCompany_name
Get ComStringOf Of hoJResp "entities[i].result.address_line1" To sResultAddress_line1
Get ComStringOf Of hoJResp "entities[i].result.city_locality" To sResultCity_locality
Get ComStringOf Of hoJResp "entities[i].result.state_province" To sResultState_province
Get ComStringOf Of hoJResp "entities[i].result.postal_code" To sResultPostal_code
Get ComStringOf Of hoJResp "entities[i].result.address_residential_indicator" To sResultAddress_residential_indicator
Get ComIntOf Of hoJResp "entities[i].result.line" To iLine
Get ComStringOf Of hoJResp "entities[i].result.type" To sV_Type
Move (i + 1) To i
Loop
End_Procedure
Curl Command
curl -X POST
-H "API-Key: {{API_KEY}}"
-H "Content-Type: application/json"
-d '{
"text": "I need to ship a 17lb package that’s 36x12x24in. It’s going to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It\'s really valuable, so insure it for $400 and require an adult signature please.",
"shipment": {
"ship_from": {
"name": "John Doe",
"company_name": "Example Corp.",
"address_line1": "4009 Marathon Blvd",
"city_locality": "Austin",
"state_province": "TX",
"postal_code": "78756",
"country_code": "US",
"phone": "512-555-5555"
}
}
}'
https://api.shipengine.com/v1/shipments/recognize
Postman Collection Item JSON
{
"name": "Parse shipment info w/ known fields",
"event": [
{
"listen": "test",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"text\": \"I need to ship a 17lb package that’s 36x12x24in. It’s going to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature please.\",\n\t\"shipment\": {\n\t\t\"ship_from\": {\n\t\t\t\"name\": \"John Doe\",\n\t\t\t\"company_name\": \"Example Corp.\",\n\t\t\t\"address_line1\": \"4009 Marathon Blvd\",\n\t\t\t\"city_locality\": \"Austin\",\n\t\t\t\"state_province\": \"TX\",\n\t\t\t\"postal_code\": \"78756\",\n\t\t\t\"country_code\": \"US\",\n\t\t\t\"phone\": \"512-555-5555\"\n\t\t}\n\t}\n}"
},
"url": {
"raw": "https://api.shipengine.com/v1/shipments/recognize",
"protocol": "https",
"host": [
"api",
"shipengine",
"com"
],
"path": [
"v1",
"shipments",
"recognize"
]
},
"description": "If you already know some parts of the shipment, you can provide them to ShipEngine, and we'll use those values. These values can also help us recognize other parts of the shipment."
},
"response": [
{
"name": "Parse shipment info w/ known fields",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"text\": \"I need to ship a 17lb package that’s 36x12x24in. It’s going to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature please.\",\n\t\"shipment\": {\n\t\t\"ship_from\": {\n\t\t\t\"name\": \"John Doe\",\n\t\t\t\"company_name\": \"Example Corp.\",\n\t\t\t\"address_line1\": \"4009 Marathon Blvd\",\n\t\t\t\"city_locality\": \"Austin\",\n\t\t\t\"state_province\": \"TX\",\n\t\t\t\"postal_code\": \"78756\",\n\t\t\t\"country_code\": \"US\",\n\t\t\t\"phone\": \"512-555-5555\"\n\t\t}\n\t}\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.shipengine.com/v1/shipments/recognize",
"protocol": "https",
"host": [
"api",
"shipengine",
"com"
],
"path": [
"v1",
"shipments",
"recognize"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Wed, 18 Sep 2019 17:13:29 GMT"
},
{
"key": "Content-Type",
"value": "application/json; charset=utf-8"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Access-Control-Allow-Origin",
"value": "https://www.shipengine.com"
},
{
"key": "Vary",
"value": "Origin"
},
{
"key": "content-encoding",
"value": "gzip"
}
],
"cookie": [
],
"body": "{\n \"score\": 0.9604458751176728,\n \"shipment\": {\n \"confirmation\": \"adult_signature\",\n \"ship_to\": {\n \"name\": \"Amanda Miller\",\n \"company_name\": \"Adult Signature\",\n \"address_line1\": \"525 Winchester Blvd\",\n \"city_locality\": \"San Jose\",\n \"state_province\": \"CA\",\n \"postal_code\": \"95128\",\n \"address_residential_indicator\": \"yes\"\n },\n \"ship_from\": {\n \"name\": \"John Doe\",\n \"company_name\": \"Example Corp.\",\n \"phone\": \"512-555-5555\",\n \"address_line1\": \"4009 Marathon Blvd\",\n \"city_locality\": \"Austin\",\n \"state_province\": \"TX\",\n \"postal_code\": \"78756\",\n \"country_code\": \"US\",\n \"address_residential_indicator\": \"unknown\"\n },\n \"packages\": [\n {\n \"weight\": {\n \"value\": 17,\n \"unit\": \"pound\"\n },\n \"dimensions\": {\n \"length\": 36,\n \"width\": 12,\n \"height\": 24,\n \"unit\": \"inch\"\n },\n \"insured_value\": {\n \"amount\": 400,\n \"currency\": \"USD\"\n }\n }\n ]\n },\n \"entities\": [\n {\n \"type\": \"weight\",\n \"score\": 0.9805313966503588,\n \"text\": \"17lb\",\n \"start_index\": 17,\n \"end_index\": 20,\n \"result\": {\n \"value\": 17,\n \"unit\": \"pound\"\n }\n },\n {\n \"type\": \"dimensions\",\n \"score\": 1,\n \"text\": \"36x12x24in\",\n \"start_index\": 37,\n \"end_index\": 46,\n \"result\": {\n \"length\": 36,\n \"width\": 12,\n \"height\": 24,\n \"unit\": \"inch\"\n }\n },\n {\n \"type\": \"dimension\",\n \"score\": 0.9805313966503588,\n \"text\": \"24in\",\n \"start_index\": 43,\n \"end_index\": 46,\n \"result\": {\n \"unit\": \"inch\",\n \"value\": 24\n }\n },\n {\n \"type\": \"address\",\n \"score\": 0.9686815805970408,\n \"text\": \"to Amanda Miller’s house at 525 Winchester Blvd in San Jose California. The zip code is 95128. It's really valuable, so insure it for $400 and require an adult signature\",\n \"start_index\": 60,\n \"end_index\": 228,\n \"result\": {\n \"direction\": \"to\",\n \"name\": \"Amanda Miller\",\n \"company_name\": \"Adult Signature\",\n \"address_line1\": \"525 Winchester Blvd\",\n \"city_locality\": \"San Jose\",\n \"state_province\": \"CA\",\n \"postal_code\": \"95128\",\n \"address_residential_indicator\": \"yes\"\n }\n },\n {\n \"type\": \"person\",\n \"score\": 0.9519646137063122,\n \"text\": \"Amanda Miller\",\n \"start_index\": 63,\n \"end_index\": 75,\n \"result\": {\n \"value\": \"Amanda Miller\"\n }\n },\n {\n \"type\": \"residential_indicator\",\n \"score\": 0.9519646137063122,\n \"text\": \"house\",\n \"start_index\": 79,\n \"end_index\": 83,\n \"result\": {\n \"value\": \"yes\"\n }\n },\n {\n \"type\": \"address_line\",\n \"score\": 0.9805313966503588,\n \"text\": \"525 Winchester Blvd\",\n \"start_index\": 88,\n \"end_index\": 106,\n \"result\": {\n \"line\": 1,\n \"value\": \"525 Winchester Blvd\"\n }\n },\n {\n \"type\": \"number\",\n \"score\": 0.9805313966503588,\n \"text\": \"525\",\n \"start_index\": 88,\n \"end_index\": 90,\n \"result\": {\n \"type\": \"cardinal\",\n \"value\": 525\n }\n },\n {\n \"type\": \"city_locality\",\n \"score\": 0.9805313966503588,\n \"text\": \"San Jose\",\n \"start_index\": 111,\n \"end_index\": 118,\n \"result\": {\n \"value\": \"San Jose\"\n }\n },\n {\n \"type\": \"state_province\",\n \"score\": 0.9805313966503588,\n \"text\": \"California\",\n \"start_index\": 120,\n \"end_index\": 129,\n \"result\": {\n \"name\": \"California\",\n \"value\": \"CA\"\n }\n },\n {\n \"type\": \"postal_code\",\n \"score\": 0.9519646137063122,\n \"text\": \"95128\",\n \"start_index\": 148,\n \"end_index\": 152,\n \"result\": {\n \"value\": \"95128\"\n }\n },\n {\n \"type\": \"insurance\",\n \"score\": 0.8530163983409642,\n \"text\": \"insure it for $400\",\n \"start_index\": 180,\n \"end_index\": 197,\n \"result\": {\n \"value\": 400,\n \"unit\": \"USD\"\n }\n },\n {\n \"type\": \"insured_value\",\n \"score\": 1,\n \"text\": \"$400\",\n \"start_index\": 194,\n \"end_index\": 197,\n \"result\": {\n \"unit\": \"USD\",\n \"value\": 400\n }\n },\n {\n \"type\": \"company\",\n \"score\": 0.9519646137063122,\n \"text\": \"adult signature\",\n \"start_index\": 214,\n \"end_index\": 228,\n \"result\": {\n \"value\": \"Adult Signature\"\n }\n },\n {\n \"type\": \"delivery_confirmation\",\n \"score\": 0.8530163983409642,\n \"text\": \"adult signature\",\n \"start_index\": 214,\n \"end_index\": 228,\n \"result\": {\n \"name\": \"Adult Signature\",\n \"value\": \"adult_signature\"\n }\n }\n ]\n}"
}
]
}