skip to Main Content

I want to fetch Products list and return as JSON using the Shopify Python API.
I tried the .to_json() function

products=shopify.Product.find().to_json()

Got the error

'PaginatedCollection' object has no attribute 'to_json'

I tried doing

products=shopify.Product.find()
js=json.dumps(products)

Error:

Object of type Product is not JSON serializable

How can I serialize the Products response to JSON ?

2

Answers


  1. Try converting it into the dictionary like this:

    product=shopify.Product.find(Remote-id of product)
    product=shopify.Product.find(124583278)
    
    product_result = product.to_dict()
    
    Login or Signup to reply.
  2. You’ll need to loop through the list,convert each to dict (to_dict() function) and append to a list.

    products=shopify.Product.find()
    productsJSON=[]
    for product in products:
        productsJSON.append(product.to_dict())
    

    You can now send productsJSON list as a JSON response.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search