Skip to content

Instantly share code, notes, and snippets.

@glombard
Created October 25, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glombard/181c13c5117daf68e2a4 to your computer and use it in GitHub Desktop.
Save glombard/181c13c5117daf68e2a4 to your computer and use it in GitHub Desktop.
Querying JSON in Python using Jinja2
# 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