PyJsonQ is an easy to use and package to query json data. It is a rewrite of the Go package "GoJSONQ" which you can find here: GoJSONQ Github Repository
I decided to rewrite this package in Python because I love it's simple API and is just a beauty to work with.
Over time I will potentially update this package and add more fun and
useful stuff to it, but for now the only thing I added are two
operators for the Where
method: holds
, notHolds
pip install pyjsonquery
First import the JsonQuery
class into your project
from pyjsonq import JsonQuery
Next create a new JsonQuery instance and load the data into the query using either File / file, String / string, TOMLFile / toml_file, TOMLString / toml_string or Raw / raw.
jq: JsonQuery = JsonQuery().String(
"""
{
"city": "dhaka",
"type": "weekly",
"temperatures": [
30,
39.9,
35.4,
33.5,
31.6,
33.2,
30.7
]
}
"""
)
# OR
jq: JsonQuery = JsonQuery().File("./file.json")
Once you created your query object you can then query over it using a variety of methods. Here is a quick example:
avg_temp: float = jq.At("temperatures").Avg()
print(avg_temp) # 33.471428571428575
city_name: str = jq.Find("city")
print(city_name) # dhaka
You can query over the json using various methods such as Find, First, Nth, Pluck, Where, OrWhere, WhereIn, Sort, SortBy, Drop, etc.
You can also aggregate your data after a query using Avg, Count, Max, Min, etc.
An overview over all query functions can be found in the wiki page