Sample code for 30+ languages & platforms
Xbase++

Shopify Get particular fields of a single product

See more Shopify Examples

Get particular fields of a single product

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL oSbJson
LOCAL oJson
LOCAL nProductId
LOCAL cProductTitle
LOCAL i
LOCAL nCount_i
LOCAL nId
LOCAL nProduct_id
LOCAL nPosition
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nWidth
LOCAL nHeight
LOCAL cSrc
LOCAL j
LOCAL nCount_j
LOCAL nIntVal

nSuccess := 0

oRest := CreateObject("Chilkat.Rest")

oRest:SetAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY")

nSuccess := oRest:Connect("chilkat.myshopify.com", 443, 1, 1)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

oSbJson := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestNoBodySb("GET", "/admin/products/#{id}.json?fields=id,images,title", oSbJson)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oSbJson:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "Received error response code: " + Str(oRest:ResponseStatusCode)
    ? "Response body:"
    ? oSbJson:GetAsString()
    oRest:destroy()
    oSbJson:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbJson)

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

nProductId := oJson:IntOf("product.id")
cProductTitle := oJson:StringOf("product.title")
i := 0
nCount_i := oJson:SizeOfArray("product.images")
DO WHILE i < nCount_i
    oJson:I := i
    nId := oJson:IntOf("product.images[i].id")
    nProduct_id := oJson:IntOf("product.images[i].product_id")
    nPosition := oJson:IntOf("product.images[i].position")
    cCreated_at := oJson:StringOf("product.images[i].created_at")
    cUpdated_at := oJson:StringOf("product.images[i].updated_at")
    nWidth := oJson:IntOf("product.images[i].width")
    nHeight := oJson:IntOf("product.images[i].height")
    cSrc := oJson:StringOf("product.images[i].src")
    j := 0
    nCount_j := oJson:SizeOfArray("product.images[i].variant_ids")
    DO WHILE j < nCount_j
        oJson:J := j
        nIntVal := oJson:IntOf("product.images[i].variant_ids[j]")
        j := j + 1
    ENDDO
    i := i + 1
ENDDO

//  A sample JSON response body that is parsed by the above code:

//  {
//    "product": {
//      "id": 632910392,
//      "title": "IPod Nano - 8GB",
//      "images": [
//        {
//          "id": 850703190,
//          "product_id": 632910392,
//          "position": 1,
//          "created_at": "2017-09-22T14:08:02-04:00",
//          "updated_at": "2017-09-22T14:08:02-04:00",
//          "width": 123,
//          "height": 456,
//          "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
//          "variant_ids": [
//          ]
//        },
//        {
//          "id": 562641783,
//          "product_id": 632910392,
//          "position": 2,
//          "created_at": "2017-09-22T14:08:02-04:00",
//          "updated_at": "2017-09-22T14:08:02-04:00",
//          "width": 123,
//          "height": 456,
//          "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano-2.png?v=1506103682",
//          "variant_ids": [
//            808950810
//          ]
//        }
//      ]
//    }
//  }

? "Example Completed."

oRest:destroy()
oSbJson:destroy()
oJson:destroy()