15

I am working on a system to output a JSON file and I use Python to parse the data and display it in a UI (PySide). I now would like to add filtering to that system and I think instead of writing a query system, if there was one out there for JSON (in Python), that would save me a lot of development time. I found this thread:

Is there a query language for JSON?

but that's more for a Web-based system. Any ideas on a Python equivalent?

EDIT [for clarity]:

The format the data that I'll be generating is like this:

{
    "Operations": [
    {
        "OpID": "0", 
        "type": "callback", 
        "stringTag1": "foo1", 
        "stringTag2": "FooMsg", 
        "Children": [...],
        "value": "0.000694053"
   },
   {
        "OpID": "1", 
        "type": "callback", 
        "stringTag1": "moo1", 
        "string2": "MooMsg", 
        "Children": [...],
        "value": "0.000468427"
   }
}

Where 'Children' could be nested arrays of the same thing (other operations). The system will be built to allow users to add their own tags as well to the data. My hope was to have a querying system that would allow users to define their own 'filters' as well, hence the question about the querying language. If there was something that would let me do something like "SELECT * WHERE "type" == "callback" and get the requisite operations back, that would be great.

The suggestion of Pync is interesting, I'll give that a look.

2
  • This seems like a fairly open-ended question that might need more research before it fits here. Commented Mar 9, 2013 at 2:41
  • Can you give an example of the json you're looking at working with, as well as an expected query? Commented Mar 9, 2013 at 3:35

5 Answers 5

16

I notice this question was asked a few years ago but if someone else find this, here are some newer projects trying to address this same problem:

I personally went with pyjq because I use jq all the time for data exploration but ObjectPath seems very attractive and not limited to json.

Sign up to request clarification or add additional context in comments.

Comments

11

I thought about this a little bit, and I lean towards something less specific such as a "JSON Query Language" and considered something more generic. I remembered from working with C# a bit that they had a somewhat generic querying system called LINQ for handling these sort of querying issues.

It looks as though Python has something similar called Pynq which supports basic querying such as:

filtered_collection = From(some_collection).where("item.property > 10").select_many()

It even appears to have some basic aggregation functions. While not being specific to JSON, I think it's a least a good starting point for querying.

1 Comment

the code hasn't been updated since 2009 and they pypi gave their package name away
3

You can also check out PythonQL, a query language extension to Python that handles SQL and JSON queries: pythonql

Comments

1

pyjsonq

https://github.com/s1s1ty/py-jsonq

from pyjsonq import JsonQ

qe = JsonQ('myfile.json')
res = qe.at('products').where('cat', '=', 2).get()
print(res)

"""
[
    {
        id: 3,
        city: 'dhk',
        name: 'Redmi 3S Prime',
        cat: 2,
        price: 12000
    },
    ...
]

I think it's important that the interaction with json is in-memory so that you can still do things manually for complex criteria

1 Comment

UPDATE: i'm having trouble accessing nested structures using this library
0

a few of these answers are a bit outdated. There's a newer/better maintained python jq implmentation, https://github.com/mwilliamson/jq.py

pip install jq

The other one isn't well maintained anymore (it's old enough it assumes you'll be using python 2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.