question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need documentation/example to make jmespath query on datetime object.

See original GitHub issue

Currently all the boto3 documentation show date element as datetime() object.e.g.

{
    'Contents': [
        {
            'Key': 'string',
            'LastModified': datetime(2015, 1, 1),
            'ETag': 'string',
  .....
}

There is little documentation mentioned how to deal with the datetime object when using jmespath query.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
tomisaacsoncommented, Aug 13, 2019

I contacted AWS support about this and after some wrong starts they gave me this which works:

import boto3 
s3 = boto3.client("s3")
s3_paginator = s3.get_paginator('list_objects_v2')
s3_iterator = s3_paginator.paginate(Bucket='mytestbucket')
filtered_iterator = s3_iterator.search(
    "Contents[?to_string(LastModified)>='\"2016-12-27 08:05:37+00:00\"'].Key"
    )
for key_data in filtered_iterator:
    print(key_data)

However because you’re providing a string you have to be careful with the format. I haven’t tested this exhaustively so there may be combinations that don’t work.

1reaction
ashish-logmastercommented, Mar 19, 2022

A clean version where you can have the filter date to be dynamic:

    my_region = os.environ.get("AWS_REGION", "us-east-1")
    session = boto3.Session(region_name=my_region)
    client = session.client("ec2")
    paginator = client.get_paginator('describe_snapshots')
    page_iterator = paginator.paginate(
        Filters=[
            {
                "Name": "status",
                "Values": [
                    "completed",
                ],
            },
        ],
        OwnerIds = ['self'],
    )

    filter_date = str(datetime.today() - timedelta(days=1))
    filtered_iterator = page_iterator.search(f"Snapshots[?to_string(StartTime) >= '\"{filter_date}\"']") 
    for k in filtered_iterator:
        pprint.pprint(k)
Read more comments on GitHub >

github_iconTop Results From Across the Web

JMESPath Tutorial
JMESPath Tutorial¶. This is a tutorial of the JMESPath language. JMESPath is a query language for JSON. You can extract and transform elements...
Read more >
python - JMESPath date filtering - Stack Overflow
You can't just put a str(compareTime) expression in as string literal and have Python understand that that's what you wanted to replace.
Read more >
Why do I only have to escape the last backslash in a string ...
I have the following Python code: localExtractpath = "D:\Python\From 0 to 1\Excel\" if os.path.exists(localZipPath): print("Cool!
Read more >
openshift-ansible-3.10.38-1.git.0.8cfad6d.el7 | Build Info
Create v3.10 imagestreams, quickstart, and db-templates that support ppc64le ... Note about jmespath requirement for control node (#599) ...
Read more >
openshift-ansible.spec · master · oit-ssi-systems ... - Duke Gitlab
RPM doesn't handle this so we have to do some pre-transaction magic. ... openshift-aws: updating the subnet querying (mwoodson@redhat.com).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found