[BangPypers] JSON PARSER (Rajiv)

Dhruv Baldawa dhruvbaldawa at gmail.com
Sat Mar 22 05:28:55 CET 2014


eval will not work always, especially when you have "null" values.

In [1]: eval('{"a": null}')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-0e9c67a4a105> in <module>()
----> 1 eval('{"a": null}')

<string> in <module>()

NameError: name 'null' is not defined


On Fri, Mar 21, 2014 at 3:26 PM, Rajiv Subramanian M
<rajiv.m1991 at gmail.com>wrote:

> why not use 'eval' keyword for parsing json..
>
> Here is an example.
>
> *with 'eval' *
>
> json_data ='''
> {
>     "firstName": "John",
>     "lastName" : "Smith",
>     "age"      : 25,
>     "address"  :
>     {
>         "streetAddress": "21 2nd Street",
>         "city"         : "New York",
>         "state"        : "NY",
>         "postalCode"   : "10021"
>     },
>     "phoneNumber":
>     [
>         {
>             "type"  : "home",
>             "number": "212 555-1234"
>         },
>         {
>             "type"  : "fax",
>             "number": "646 555-4567"
>         }
>     ]
> }'''
> d = eval('('+json_data+')')
> for key in d:
>     print(key, ':', d[key].__class__, d[key])
>
> *Output:*
>
> lastName : <class 'str'> Smith
> age : <class 'int'> 25
> phoneNumber : <class 'list'> [{'type': 'home', 'number': '212
> 555-1234'}, {'type': 'fax', 'number': '646 555-4567'}]
> firstName : <class 'str'> John
> address : <class 'dict'> {'postalCode': '10021', 'city': 'New York',
> 'streetAddress': '21 2nd Street', 'state': 'NY'}
>
>
>
>
> On Fri, Mar 21, 2014 at 2:45 PM, <bangpypers-request at python.org> wrote:
>
> > Send BangPypers mailing list submissions to
> >         bangpypers at python.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >         https://mail.python.org/mailman/listinfo/bangpypers
> > or, via email, send a message with subject or body 'help' to
> >         bangpypers-request at python.org
> >
> > You can reach the person managing the list at
> >         bangpypers-owner at python.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of BangPypers digest..."
> >
> >
> > Today's Topics:
> >
> >    1. JSON PARSER (lokesh bobby)
> >    2. Re: JSON PARSER (Noufal Ibrahim KV)
> >    3. Re: JSON PARSER (Prashant Gaur)
> >    4. Re: JSON PARSER (Vishal)
> >    5. Re: JSON PARSER (lokesh bobby)
> >    6. Re: JSON PARSER (Jayanth Koushik)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Fri, 21 Mar 2014 16:18:40 +0800 (SGT)
> > From: lokesh bobby <lokeshbobbys at yahoo.com>
> > To: Bangalore Python Users Group - India <bangpypers at python.org>
> > Subject: [BangPypers] JSON PARSER
> > Message-ID:
> >         <1395389920.23044.YahooMailNeo at web193806.mail.sg3.yahoo.com>
> > Content-Type: text/plain; charset=utf-8
> >
> > Hi ALL,
> >
> > Can you share your thoughts on how to parse a JSON file by using python?
> >
> > Thanks,
> > Lokesh.
> >
> > ------------------------------
> >
> > Message: 2
> > Date: Fri, 21 Mar 2014 14:08:27 +0530
> > From: Noufal Ibrahim KV <noufal at nibrahim.net.in>
> > To: lokesh bobby <lokeshbobbys at yahoo.com>
> > Cc: Bangalore Python Users Group - India <bangpypers at python.org>
> > Subject: Re: [BangPypers] JSON PARSER
> > Message-ID: <878us4c5fg.fsf at sanitarium.localdomain>
> > Content-Type: text/plain
> >
> > On Fri, Mar 21 2014, lokesh bobby wrote:
> >
> > > Hi ALL,
> > >
> > > Can you share your thoughts on how to parse a JSON file by using
> > > python?
> >
> > import json
> >
> > with open("data.json") as f:
> >      json.load(f)
> >
> >
> > [...]
> >
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> >
> >
> > ------------------------------
> >
> > Message: 3
> > Date: Fri, 21 Mar 2014 14:30:19 +0530
> > From: Prashant Gaur <91prashantgaur at gmail.com>
> > To: Bangalore Python Users Group - India <bangpypers at python.org>
> > Subject: Re: [BangPypers] JSON PARSER
> > Message-ID:
> >         <
> > CABewc72XDpEESfCvSCXzq5GPUfer1q9z2RMYp8veEfDftmpuBA at mail.gmail.com>
> > Content-Type: text/plain; charset=ISO-8859-1
> >
> > Python provides json module to parse JSON files.
> >
> > import json
> > from pprint import pprint
> > with open('data.json') as json_file:
> >     data = json.load(json_file)
> > pprint(data)
> >
> > Here i am using pprint to print result data (You can have a look
> > http://docs.python.org/2/library/pprint.html )
> > Or
> > You can also use simplejson library to do same  . (
> > http://simplejson.readthedocs.org/en/latest/ )
> >
> >
> >
> >
> > On Fri, Mar 21, 2014 at 2:08 PM, Noufal Ibrahim KV
> > <noufal at nibrahim.net.in>wrote:
> >
> > > On Fri, Mar 21 2014, lokesh bobby wrote:
> > >
> > > > Hi ALL,
> > > >
> > > > Can you share your thoughts on how to parse a JSON file by using
> > > > python?
> > >
> > > import json
> > >
> > > with open("data.json") as f:
> > >      json.load(f)
> > >
> > >
> > > [...]
> > >
> > >
> > > --
> > > Cordially,
> > > Noufal
> > > http://nibrahim.net.in
> > > _______________________________________________
> > > BangPypers mailing list
> > > BangPypers at python.org
> > > https://mail.python.org/mailman/listinfo/bangpypers
> > >
> >
> >
> >
> > --
> > Prashant Gaur
> >
> > Mobile : +91 9717353657
> > http://gaurprashant.blogspot.in/
> > http://stackoverflow.com/users/1850358/prashant-gaur
> > http://www.about.me/prashantgaur/
> >
> >
> > ------------------------------
> >
> > Message: 4
> > Date: Fri, 21 Mar 2014 14:31:38 +0530
> > From: Vishal <vsapre80 at gmail.com>
> > To: Bangalore Python Users Group - India <bangpypers at python.org>
> > Subject: Re: [BangPypers] JSON PARSER
> > Message-ID:
> >         <
> > CACPguY8LrLjgKHytdr+2X7zHenDcYp15e1i7xFvA-FA+pSCJNQ at mail.gmail.com>
> > Content-Type: text/plain; charset=UTF-8
> >
> > Hi,
> > I have used simplejson and ultrajson. Found ultrajson to be the fastest
> > amongst known libraries for Python
> >
> > UltraJSON (
> >
> http://pushingtheweb.com/2011/03/ultra-fast-json-encoding-decoding-python/
> > )
> >
> > Compare performance: http://stackoverflow.com/a/15440843
> >
> > Download it from here: https://pypi.python.org/pypi/ujson/
> >
> > Take care,
> > Vishal
> >
> >
> >
> >
> > Thanks and best regards,
> > Vishal Sapre
> >
> > ---
> > "Life is 10% how you make it, and 90% how you take it"
> > "????? ?????, ????? ????? (Benefit for most people, Happiness for most
> > people.)"
> > ---
> > Please DONT print this email, unless you really need to. Save Energy &
> > Paper. Save the Earth.
> >
> >
> > On Fri, Mar 21, 2014 at 2:08 PM, Noufal Ibrahim KV
> > <noufal at nibrahim.net.in>wrote:
> >
> > > On Fri, Mar 21 2014, lokesh bobby wrote:
> > >
> > > > Hi ALL,
> > > >
> > > > Can you share your thoughts on how to parse a JSON file by using
> > > > python?
> > >
> > > import json
> > >
> > > with open("data.json") as f:
> > >      json.load(f)
> > >
> > >
> > > [...]
> > >
> > >
> > > --
> > > Cordially,
> > > Noufal
> > > http://nibrahim.net.in
> > > _______________________________________________
> > > BangPypers mailing list
> > > BangPypers at python.org
> > > https://mail.python.org/mailman/listinfo/bangpypers
> > >
> >
> >
> > ------------------------------
> >
> > Message: 5
> > Date: Fri, 21 Mar 2014 17:00:03 +0800 (SGT)
> > From: lokesh bobby <lokeshbobbys at yahoo.com>
> > To: Noufal Ibrahim KV <noufal at nibrahim.net.in>
> > Cc: Bangalore Python Users Group - India <bangpypers at python.org>
> > Subject: Re: [BangPypers] JSON PARSER
> > Message-ID:
> >         <1395392403.5407.YahooMailNeo at web193803.mail.sg3.yahoo.com>
> > Content-Type: text/plain; charset=iso-8859-1
> >
> > Hi Noufal,
> >
> > Thanks for your reply. I am not looking for loading the JSON file. There
> > is a limitation in it. Go thru the links
> >
> >
> http://docs.python.org/2/library/json.html#repeated-names-within-an-object
> >
> >
> http://docs.python.org/3.2/library/json.html#repeated-names-within-an-object
> >
> > In order to get rid of that problem, I am looking for some JSON stream
> > parsers.
> >
> > Thanks,
> > Lokesh.
> >
> >
> >
> > On Friday, 21 March 2014 2:09 PM, Noufal Ibrahim KV <
> > noufal at nibrahim.net.in> wrote:
> >
> > On Fri, Mar 21 2014, lokesh bobby wrote:
> >
> >
> > > Hi ALL,
> > >
> > > Can you share your thoughts on how to parse a JSON file by using
> > > python?
> >
> > import json
> >
> > with open("data.json") as f:
> > ? ?  json.load(f)
> >
> >
> > [...]
> >
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> >
> > ------------------------------
> >
> > Message: 6
> > Date: Fri, 21 Mar 2014 14:45:19 +0530
> > From: Jayanth Koushik <jnkoushik at gmail.com>
> > To: lokesh bobby <lokeshbobbys at yahoo.com>,  Bangalore Python Users
> >         Group - India <bangpypers at python.org>
> > Subject: Re: [BangPypers] JSON PARSER
> > Message-ID:
> >         <
> > CA+pEiEJF+VhmxvCh5Ef6oPVc5MBavxZ5wEoDnz2hugQ_-NtTqA at mail.gmail.com>
> > Content-Type: text/plain; charset=ISO-8859-1
> >
> > Hi Lokesh
> >
> > The 'problem' that you talk about isn't really a problem. Since the JSON
> > specification does not say what is to be done for repeated names, it is
> up
> > to the implementation to decide. What is your requirement for handling
> > repeated names?
> >
> > Jayanth
> >
> >
> > On Fri, Mar 21, 2014 at 2:30 PM, lokesh bobby <lokeshbobbys at yahoo.com
> > >wrote:
> >
> > > Hi Noufal,
> > >
> > > Thanks for your reply. I am not looking for loading the JSON file.
> There
> > > is a limitation in it. Go thru the links
> > >
> > >
> >
> http://docs.python.org/2/library/json.html#repeated-names-within-an-object
> > >
> > >
> >
> http://docs.python.org/3.2/library/json.html#repeated-names-within-an-object
> > >
> > > In order to get rid of that problem, I am looking for some JSON stream
> > > parsers.
> > >
> > > Thanks,
> > > Lokesh.
> > >
> > >
> > >
> > > On Friday, 21 March 2014 2:09 PM, Noufal Ibrahim KV <
> > > noufal at nibrahim.net.in> wrote:
> > >
> > > On Fri, Mar 21 2014, lokesh bobby wrote:
> > >
> > >
> > > > Hi ALL,
> > > >
> > > > Can you share your thoughts on how to parse a JSON file by using
> > > > python?
> > >
> > > import json
> > >
> > > with open("data.json") as f:
> > >      json.load(f)
> > >
> > >
> > > [...]
> > >
> > >
> > > --
> > > Cordially,
> > > Noufal
> > > http://nibrahim.net.in
> > > _______________________________________________
> > > BangPypers mailing list
> > > BangPypers at python.org
> > > https://mail.python.org/mailman/listinfo/bangpypers
> > >
> >
> >
> > ------------------------------
> >
> > Subject: Digest Footer
> >
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> >
> > ------------------------------
> >
> > End of BangPypers Digest, Vol 79, Issue 8
> > *****************************************
> >
>
>
>
> --
> Rajiv M
> Software Engineer.
>
> *DoubleSpring Media (P) Ltd.*   #15/1 Robertson Road, Frazer Town,
> Bangalore 05, IND.
> Office: +91-80-40917126,  Mobile: +91 7411 129611,  Skype:
> rajiv.m1991<http://lightness-rejoice.rhcloud.com>,
>  Web: www.doublespring.com.
>
>  <http://twitter.com/rajivm1991>  <http://facebook.com/rajivm1991>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Dhruv Baldawa
(http://www.dhruvb.com)


More information about the BangPypers mailing list