Sample code for 30+ languages & platforms
PureBasic

Shopify Get particular fields of a single product

See more Shopify Examples

Get particular fields of a single product

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_KEY")

    success = CkRest::ckConnect(rest,"chilkat.myshopify.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/admin/products/#{id}.json?fields=id,images,title",sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response body:"
        Debug CkStringBuilder::ckGetAsString(sbJson)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbJson)

    ; The following code parses the JSON response.
    ; A sample JSON response is shown below the sample code.
    productId.i
    productTitle.s
    i.i
    count_i.i
    id.i
    product_id.i
    position.i
    created_at.s
    updated_at.s
    width.i
    height.i
    src.s
    j.i
    count_j.i
    intVal.i

    productId = CkJsonObject::ckIntOf(json,"product.id")
    productTitle = CkJsonObject::ckStringOf(json,"product.title")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"product.images")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        id = CkJsonObject::ckIntOf(json,"product.images[i].id")
        product_id = CkJsonObject::ckIntOf(json,"product.images[i].product_id")
        position = CkJsonObject::ckIntOf(json,"product.images[i].position")
        created_at = CkJsonObject::ckStringOf(json,"product.images[i].created_at")
        updated_at = CkJsonObject::ckStringOf(json,"product.images[i].updated_at")
        width = CkJsonObject::ckIntOf(json,"product.images[i].width")
        height = CkJsonObject::ckIntOf(json,"product.images[i].height")
        src = CkJsonObject::ckStringOf(json,"product.images[i].src")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"product.images[i].variant_ids")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            intVal = CkJsonObject::ckIntOf(json,"product.images[i].variant_ids[j]")
            j = j + 1
        Wend
        i = i + 1
    Wend

    ; 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
    ;         ]
    ;       }
    ;     ]
    ;   }
    ; }

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure