pynamodb / PynamoDB

A pythonic interface to Amazon's DynamoDB
http://pynamodb.readthedocs.io
MIT License
2.43k stars 427 forks source link

Possible to return results in standard json? #882

Open softwareguy74 opened 3 years ago

softwareguy74 commented 3 years ago

I'd like to return the results of a query as standard json objects rather than DynamoDB json. Is this possible? It so, how?

bnssoftware commented 3 years ago

Any word on this?

jpinner-lyft commented 3 years ago

There is no out-of-the-box way to do this. Depending on the transformations you need you could query using the model interface and then iterate through and convert each model to a dictionary, or you could query using the table api and iterate through the raw results, transforming the dynamo json as necessary.

Related you can look here for an attempt to add to/from json methods to the model objects. https://github.com/pynamodb/PynamoDB/pull/857

bnssoftware commented 3 years ago

I think that is highly inefficient. If you are able to do the conversion with .get() on a single item from DynamoDB to JSON internally, why can't you guys create an internal iterator that does the same on .query() or .scan()?

jpinner-lyft commented 3 years ago

If you don't care about the model abstraction then using the low-level connection api is likely the best option: https://github.com/pynamodb/PynamoDB/blob/master/docs/low_level.rst

It should be easy to combine that with a ResultIterator where you can provide your own mapping function to transform the DynamoDB item into a dictionary: https://github.com/pynamodb/PynamoDB/blob/master/pynamodb/pagination.py#L153

As might be evident in the linked PR above, removing the dynamodb types is easy but converting into the correct python type (e.g. lists vs sets) requires type information from the model.

bnssoftware commented 3 years ago

I just want the following returned when issuing a scan or query:

[
    {"id":"1", "name":"product 1"},
    {"id":"2","name":"product 2"}
]

Is this possible?

softwareguy74 commented 3 years ago

The DynamoDB boto API has a higher level table object to work with which returns standard json. Maybe we could use that behind the scenes for pynamodb?

jpinner-lyft commented 3 years ago

I'm not too familiar with boto's higher-level API, though I am happy to review pull requests :) I don't have a good sense of the flexibility we have here without breaking backwards-compatibility.

bnssoftware commented 3 years ago

Maybe add an additional optional parameter in the model definition to use table API instead? I believe it's mostly same as lower level except that it returns standard, non DynamoDB json instead.

On Mon, Nov 9, 2020, 7:51 AM jpinner-lyft notifications@github.com wrote:

I'm not too familiar with boto's higher-level API, though I am happy to review pull requests :) I don't have a good sense of the flexibility we have here without breaking backwards-compatibility.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pynamodb/PynamoDB/issues/882#issuecomment-724099146, or unsubscribe https://github.com/notifications/unsubscribe-auth/APCGJAVVPJYIHYBQBC2MBRDSPAFYPANCNFSM4TLG6LPA .

ikonst commented 3 years ago

Generally the task of an ORM is to map raw data using a schema. If you don't need or care about a schema, perhaps using boto directly would in fact be what you want?