Created
October 25, 2015 22:17
-
-
Save glombard/181c13c5117daf68e2a4 to your computer and use it in GitHub Desktop.
Querying JSON in Python using Jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Experimenting with one easy way to query a JSON document using a text query | |
# similar to how XPath works for XML docs. | |
# Jinja2 has a powerful expression syntax for evalulating/querying a Python dict. | |
# In this case, we want to query Maven Central to determine if the latest | |
# version of Robolectric is 3.0 ... | |
import requests | |
from jinja2 import Environment | |
response = requests.get( | |
'http://search.maven.org/solrsearch/select?q=g:"org.robolectric"+AND+a:"robolectric"&rows=1&wt=json') | |
template = Environment().from_string("{{ response.docs[0].latestVersion == '3.0' }}") | |
result = template.render(response.json()) | |
assert result == 'True' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment