[BangPypers] JSON PARSER (Rajiv)

Rajiv Subramanian M rajiv.m1991 at gmail.com
Fri Mar 21 13:58:15 CET 2014


Hi Jayanth,

I didn't get what you mean duplicate in json data...

And the output for the following
>>> eval("{'a': 1, 'b': 2}")

will be {'a': 1, 'b': 2}

It can't be {'a': 1}

because I believe eval() takes string argument and it will throw exception
only if that argument string doesn't met the valid syntax of Python..

Please provide an example json-data with duplicates.. so that I could
understand

On Fri, Mar 21, 2014 at 3:32 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. Re: JSON PARSER (Rajiv) (Rajiv Subramanian M)
>    2. Re: JSON PARSER (Rajiv) (Jayanth Koushik)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 21 Mar 2014 15:26:37 +0530
> From: Rajiv Subramanian M <rajiv.m1991 at gmail.com>
> To: bangpypers at python.org
> Subject: Re: [BangPypers] JSON PARSER (Rajiv)
> Message-ID:
>         <
> CAELut3XeKoUE8LBFWPyTROOMaXgNm4M_w_pQE-VAEoXd9nbhCw at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
>
> 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'}
>
> --
> 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>
>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 21 Mar 2014 15:32:13 +0530
> From: Jayanth Koushik <jnkoushik at gmail.com>
> To: Bangalore Python Users Group - India <bangpypers at python.org>
> Subject: Re: [BangPypers] JSON PARSER (Rajiv)
> Message-ID:
>         <
> CA+pEiEKbU2L30EOiqmfK6Wh-y7jVh3Fu5JxzGsM1fCQY-tzzEQ at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Sorry, I meant:
>
> >>> eval("{'a': 1, 'a': 2}")
> {'a': 2}
>
>
> On Fri, Mar 21, 2014 at 3:31 PM, Jayanth Koushik <jnkoushik at gmail.com
> >wrote:
>
> > Hi Rajiv
> >
> > eval does not work with duplicates.
> >
> > >>> eval("{'a': 1, 'b': 2}")
> > {'a': 1}


More information about the BangPypers mailing list