Skip to navigation

Convert SDK Objects to JSON

Page through every plate in a tenant and serialize the returned SDK models to a JSON file.

This example reads every plate in a tenant with the V3 Python SDK and converts the resulting SDK objects to JSON. Plates are just the example; the same pattern works for any object the SDK returns, because every V3 SDK model is a pydantic model.

The V3 plate list endpoint is beta. Set early_access="true" on the client, as in the example below, or send the EARLY-ACCESS: true header on raw HTTP requests. See the V3 Stability Guidelines.

  1. Initialize the environment

    Import the SDK and json, then create a Benchling client. Pass your tenant’s root URL and your API key; the SDK adds /api/v3 itself.

  2. List plates

    plate.list() requests the first page straight away and returns a pager that fetches the rest as you go.

  3. Loop through the results

    iter_pages() walks each page of results, and the inner loop walks the individual plates in that page’s items (50 per page by default, up to 100 with page_size). To skip the paging detail, iterate plates directly instead. See Pagination.

  4. Convert each plate

    dict(mode="json") turns an SDK model into a plain dictionary that json can write: camelCase keys, as in the API response, and only the fields the API returned. Collecting them in serialized_plates makes the whole set easy to serialize at the end, but this is the point where you would instead reshape the data into whatever your use case needs.

  5. Write the data to a file

    The response tab shows the shape of the output. In V3, wells is a link rather than the well contents; call benchling.plate.list_wells() with the plate’s ID to read them.

import json
from benchling_v3_sdk import Benchling
benchling = Benchling(
base_url="https://yourcompany.benchling.com",
api_key="<your-api-key>",
early_access="true",
)
plates = benchling.plate.list()
serialized_plates = []
for page in plates.iter_pages():
for plate in page.items:
serialized_plates.append(plate.dict(mode="json"))
with open("output.json", "w") as f:
json.dump(serialized_plates, f, indent=2)

Other ways to serialize a model

The V3 SDK models are pydantic v2 models with snake_case attribute names and camelCase aliases, so the output depends on which method you call:

CallKeysFields included
plate.dict(mode="json")camelCaseOnly fields the API returned
plate.json()camelCase, as a JSON stringOnly fields the API returned
plate.model_dump(mode="json", by_alias=True)camelCaseEvery field on the model, null where the API didn’t return it
plate.model_dump(mode="json")snake_case, with typename and schema_Every field on the model

mode="json" converts values such as datetimes to strings. Without it, a model that holds a datetime produces a dictionary that json.dumps() can’t write.