[BangPypers] JSON PARSER

2014-03-21 Thread lokesh bobby
Hi ALL, Can you share your thoughts on how to parse a JSON file by using python? Thanks, Lokesh. ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Noufal Ibrahim KV
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 ___

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Prashant Gaur
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

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Vishal
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:

Re: [BangPypers] JSON PARSER

2014-03-21 Thread lokesh bobby
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

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Jayanth Koushik
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

Re: [BangPypers] JSON PARSER

2014-03-21 Thread lokesh bobby
Hi Jayanth, Ideally speaking a JSON shouldn't be created with repetitive key names. But manually it is possible that a proper JSON file can be appended with a duplicate key. We need to catch that duplicate key. If we are going to use json.load(), the repetitive keys of the JSON file wont get

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Prashant Gaur
Hi Lokesh, we can pass lookup while parsing your json file which will make sure that name is repetitive or not . import json def duplicate_checking_hook(pairs): ''' lookup for duplicate names''' result = dict() for key, val in pairs: if key in result: raise

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Jayanth Koushik
Hi Prashant I think he wants the duplicates to be loaded, not cause an exception. Jayanth On Fri, Mar 21, 2014 at 3:15 PM, Prashant Gaur 91prashantg...@gmail.comwrote: Hi Lokesh, we can pass lookup while parsing your json file which will make sure that name is repetitive or not . import

Re: [BangPypers] JSON PARSER

2014-03-21 Thread lokesh bobby
Hi Jayanth/Prashant, Either the duplicates to be loaded or An ouput of all the duplicate key names in a JSON file should be fine for me :-) NOTE: JSON file consits of more than 2 lakhs LOC Lokesh On Friday, 21 March 2014 3:17 PM, Jayanth Koushik jnkous...@gmail.com wrote: Hi Prashant I

Re: [BangPypers] JSON PARSER (Rajiv)

2014-03-21 Thread Rajiv Subramanian M
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,

Re: [BangPypers] JSON PARSER (Rajiv)

2014-03-21 Thread Jayanth Koushik
Sorry, I meant: eval({'a': 1, 'a': 2}) {'a': 2} On Fri, Mar 21, 2014 at 3:31 PM, Jayanth Koushik jnkous...@gmail.comwrote: Hi Rajiv eval does not work with duplicates. eval({'a': 1, 'b': 2}) {'a': 1} On Fri, Mar 21, 2014 at 3:26 PM, Rajiv Subramanian M rajiv.m1...@gmail.com wrote:

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Prashant Gaur
Hello Lokesh , as we know json.loads return data in form of dict and dict can never have same keys . so we can do one thing and that is to return a list of all values which are having same names. import simplejson as json from collections import defaultdict def

Re: [BangPypers] JSON PARSER (Rajiv)

2014-03-21 Thread Jayanth Koushik
Hi Rajiv eval does not work with duplicates. eval({'a': 1, 'b': 2}) {'a': 1} On Fri, Mar 21, 2014 at 3:26 PM, Rajiv Subramanian M rajiv.m1...@gmail.comwrote: why not use 'eval' keyword for parsing json.. Here is an example. *with 'eval' * json_data =''' { firstName: John,

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Prashant Gaur
EDIT : import simplejson as json from collections import defaultdict def duplicate_key_lookup(ordered_pairs): Convert duplicate keys values to lists. # read all values into lists d = defaultdict(list) for k, v in ordered_pairs: d[k].append(v) # unpack lists

Re: [BangPypers] JSON PARSER

2014-03-21 Thread lokesh bobby
Hi Prashant, I have 2 concerns here 1. As I mentioned earlier, my file consists of more than 2 lac lines. So it is not possible to load that many number of lines inside json.loads(). We need to pass the file, which itself is a limitation as explained earlier. 2. It is not an ideal solution

Re: [BangPypers] JSON PARSER

2014-03-21 Thread Noufal Ibrahim KV
On Fri, Mar 21 2014, lokesh bobby wrote: Hi Prashant, I have 2 concerns here 1. As I mentioned earlier, my file consists of more than 2 lac lines. So it is not possible to load that many number of lines inside json.loads(). We need to pass the file, which itself is a limitation as

Re: [BangPypers] JSON PARSER (Rajiv)

2014-03-21 Thread Rajiv Subramanian M
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

Re: [BangPypers] JSON PARSER (Rajiv)

2014-03-21 Thread Dhruv Baldawa
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