Best way to list image URLs in a json

Issue

I know this is more of a python question than a fastAPI question. I want to know which method is the best way to list my image urls in a json and how to implement it in my json response.

This is my current json data:

"imgs_url": [
      "http://10.0.2.2:8000/static/product_images/f3e6421737643e583a1d.jpg, http://10.0.2.2:8000/static/product_images/b53bf8aeb27d27a739cc.jpg, http://10.0.2.2:8000/static/product_images/75a80e7c04ebaed3e425.jpg"
    ]

This is the python function i am using to get the firt list:

async def create(
    name: str = Form(...),
    price: float = Form(...),
    description: str = Form(...),
    files: List[UploadFile] = File(...),
    db: Session = Depends(get_db),
    current_user: Vendor = Depends(get_current_active_user)
):
    fileList = []
    for file in files:
        try:
            FILEPATH = "./static/product_images/"
            pimage_name = FILEPATH + imghex(file.filename)
            contents = await file.read()
            with open(pimage_name, 'wb') as f:
                f.write(contents)
        except Exception:
            return {"message": "There was an error uploading the file(s)"}
        finally:
            await file.close()
        # fileList.append("localhost:8000" + pimage_name[1:])
        fileList.append("http://10.0.2.2:8000" + pimage_name[1:])

    file_urls = ", ".join(fileList)
    new_item = Product(
        name=name,
        price=price,
        description=description,
        imgs_url=[file_urls],
        owner_id=current_user.id
    )

    db.add(new_item)
    db.commit()
    db.refresh(new_item)
    return new_item

How do i change the current list to this:

"imgs_url": [
  "http://10.0.2.2:8000/static/product_images/f3e6421737643e583a1d.jpg", 
  "http://10.0.2.2:8000/static/product_images/b53bf8aeb27d27a739cc.jpg", 
  "http://10.0.2.2:8000/static/product_images/75a80e7c04ebaed3e425.jpg"
],

also this is another option for listing images:

"imgs_url" : [
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-1.jpg"},
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-2.jpg"},
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-3.jpg"},
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-4.jpg"},
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-5.jpg"},
 {"url":"https://s3-us-west-2.amazonaws.com/appsdeveloperblog.com/images/cats/cat-6.jpg"}
]

I have three questions:
1) Please, how do i change to the second or the 3rd method?
2) Which is the best method for lisiting images in a url?
3) Is there an industry accepted standard for lisitng image urls in a json?

Solution

Remove this line that joins list element explicitly:

file_urls = ", ".join(fileList)

Change this new_item assignment to this

new_item = Product(
        name=name,
        price=price,
        description=description,
        imgs_url= fileList,  #change here 
        owner_id=current_user.id
    )

If you need the URL format as expected in #3 in the question use dictionary comprehension

fileList = [{"URL": elem } for elem in fileList]
#this will produce [{'URL': 'http://10.0.2.2:8000/static/product_images/f3e6421737643e583a1d.jpg'},...]
new_item = Product(
    name=name,
    price=price,
    description=description,
    imgs_url= fileList,  #change here 
    owner_id=current_user.id
)

For questions 2 and 3, it’s basically the consumer preference. img_urls=[<img_uurl_1>, ...] which can be iterated and used for image URLs only. If you have to provide other metadata alongside the image URL then the latter approach of having [{'URL': 'http://10.0.2.2:8000/static/product_images/f3e6421737643e583a1d.jpg'},...] is suitable.

Answered By – ShivaGaire

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published