Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for an object in a list inside an object DynamoDB and Python

I need some help to get an item from a nested JSONschema in DynamoDB. I will explain to you the schema and you can tell me if it's possible.

The schema is:

{
    "updated_at": "2018/05/02 08:32:10",
    "created_at": "2018/05/02 08:32:10",
    "updated_by": "igor",
    "created_by": "igor",
    "application": [
        {
            "name": "driver app",
            "features": [
                {
                    "name": "passenger list",
                    "settings": [],
                    "description": "feature for passenger list",
                    "id": 2
                }
            ],
            "id": 1,
            "url": "play store"
        },
        {
            "name": "passsenger app",
            "features": [],
            "id": 2,
            "url": "play store"
        }
    ],
    "address": "New York",
    "id": 4,
    "url": "https://airlink.moovex.com",
    "name": "airlink",
    "service_locations": [
        {
            "title": "IL",
            "latitude": 32,
            "longitude": 35
        }
    ]
}

I need to fetch from my application list the object by id with a query.

like image 537
Igor Kurylo Avatar asked Dec 19 '25 09:12

Igor Kurylo


1 Answers

I'm not sure this answers your question but, if you need to retrieve the application list for an object with a specific id, that's what you would do

import boto3

DyDB = boto3.resource('dynamodb')

table = DyDB.Table('YourTableName')

response = table.query(
    ProjectionExpression='application',
    KeyConditionExpression=Key('id').eq(4) # where 4 is the id you need to query
)

# this is just to test the result
for app in response['Items'][0]['application']:
    print(app['id'])

The response will give you back (in the list Items), the application attribute with the list of applications inside it

like image 58
Gabriel Avatar answered Dec 21 '25 22:12

Gabriel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!