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
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
___
Bang
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
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.pyth
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 ri
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 bo
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 lo
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 KeyErr
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.com>wrote:
> Hi Lokesh,
>
> we can pass lookup while parsing your json file which will make sure that
> name is repetitive or not .
>
>
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 wrote:
Hi Prashant
I think he wants th
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":
Sorry, I meant:
>>> eval("{'a': 1, 'a': 2}")
{'a': 2}
On Fri, Mar 21, 2014 at 3:31 PM, Jayanth Koushik wrote:
> 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> wrot
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 duplicate_key_lookup(ordered_pair
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
wrote:
> why not use 'eval' keyword for parsing json..
>
> Here is an example.
>
> *with 'eval' *
>
> json_data ='''
> {
> "firstName": "John",
> "las
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 list
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 for
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
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 v
eval will not work always, especially when you have "null" values.
In [1]: eval('{"a": null}')
---
NameError Traceback (most recent call last)
in ()
> 1 eval('{"a": null}')
in ()
NameErr
19 matches
Mail list logo