On 6 April 2014 10:56, Andrew Barnert <abarnert@yahoo.com> wrote:
From: Anoop Thomas Mathew <atmb4u@gmail.com>
Sent: Saturday, April 5, 2014 7:58 PM


>I went through the state of cpython JSON parser, and found that there is a scope for improvement in the parser interface.


>I have been thinking of implementing a class called QuickParse inside json module, which will tidy up the process of finding node, and cuts down a number of for loops, if .. else and try ... except blocks.
>
>For eg.
>
>sample_json = {
>'name' : 'John Doe',
>'age' : 45,
>'level': 'Expert',
>'languages': [
>              'C',
>              'C++',
>              'Python',
>              'Clojure'
>              ],
>    'account details': {
>         'account number': 1233312312,
>         'account balance': 1000000000.00
>     }
>}

That's not a JSON object, that's just a plain old Python dict. It doesn't even have anything to do with JSON (except the variable name, which is misleading).

And you don't need to "parse" a dict; you just use [] or get on it.

Even if you actually had a JSON string or file serializing this dict, you could get exactly the dict by just calling json.loads or json.load on it. It's hard to imagine anything simpler than that.

Compare your desired code to the equivalents that already work today:

>json_doc = QuickParse(sample_json)
>
>json_doc.get(["name"])
>'John Doe'

sample_json['name']
... or ...
sample_json.get('name')

Note that, besides the simple and more flexible access, you already didn't need that extra step of building a QuickParse object.

>json_doc.get(["languages", 2])

> 'C++'


sample_json['languages'][2]

>json_doc.get(["account details", "account balance"])
>1000000000.00


sample_json['account details']['account balance']

>json_doc.get(["notpresent"]) 
>None


sample_json.get('notpresent')

I completely agree to your point. But what if the document is of something like this:

person['country']['state']['city']['county']



>This is something I've been using in many number of projects, due to the complexity of the json documents these days. Also, there is a plan to provide option to use functions for iterating over the document, as well as selection ranges for expected values.


You can already iterate over a dict, and again, I can't imagine how anything you design will be simpler than what you can already do.

Also, at the start, you suggested that this would be an improvement to the JSON _parser_. But the stuff you showed has nothing to do with the parser. Unless the result of calling QuickParse is not JSON parsed into something dict-like, but rather something that holds the original JSON and re-parses it for each "get" request?

I'm going to assume that the whole bit about parsing is a red herring, and the part you actually want is the ability to call the "get" function with multiple keys and recursively walk the structure. I'm sure you can find recipes for that on ActiveState or PyPI, but it's also very trivial to write yourself:

 
    def get(obj, *keys, default=None):
        for key in keys:
            try:
                obj = obj[key]
            except LookupError:
                return default
        return obj

I apologize for making the previous mail complicated. You have pinned down what I was suggesting.

My suggestion is to integrate something similar as you suggested above to the cpython json module.

Won't that be a good feature for developers to be able to write code without worrying if it going to fail and writing may try.. except or if else to handle those edge cases? 

I definitely feel that it would be a great feature which is common for any program which is parsing multilevel json documents.

 
Now, without needing any kind of special object, you can write this:

    get(sample_json, 'languages', 2)