classification
Title: High Level API for json file parsing
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, methane, remi.lapeyre, rhettinger, serhiy.storchaka, xtreak, ys19991
Priority: normal Keywords: patch

Created on 2020-07-12 13:20 by ys19991, last changed 2020-07-13 13:01 by methane. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21453 closed ys19991, 2020-07-12 13:25
Messages (5)
msg373552 - (view) Author: Wansoo Kim (ys19991) * Date: 2020-07-12 13:20
Many Python users use the following snippets to read Json File.

```
with oepn(filepath, 'r') as f:
    data = json.load(f)
```

I suggest providing this snippet as a function.


```
data = json.read(filepath)
```

Reading Json is very frequent task for python users. I think it is worth providing this with the High Level API.
msg373553 - (view) Author: RĂ©mi Lapeyre (remi.lapeyre) * Date: 2020-07-12 13:28
Hi, using a file object is very common as it makes it possible to use something that is not a file, like an HTTP request or something already in memory. It makes the module serializing / de-serializing the data completely agnostic with regard to the actual physical storage which has some advantages, for example it will not raise FileNotFound.

If you want a oneliner to load data you already can use:



    with open(filepath) as f: data = json.load(f)
msg373555 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-12 14:35
There was a previous issue to support filepath for json.load https://bugs.python.org/issue36378 . This just expands the json API that could already be done using one more operation.
msg373559 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-07-12 15:52
json.load() is already a high level API. json.JSONDecoder is more low level API.

Not every two lines of code should be added as a function in the stdlib. Also, such API would be too complex because you would need to combine parameters of open() (8 parameters) and json.load() (7 parameters). If you use these two lines many times in your code you can just add a simple function that supports only options needed for you.
msg373602 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-07-13 13:01
(Off topic) There is a very common mistake in this code:

```
with oepn(filepath, 'r') as f:
    data = json.load(f)
```

JSON file is encoded in UTF-8. But the default text encoding is locale encoding.

You should do this instead:

```
with oepn(filepath, 'rb') as f:
    data = json.load(f)
```

This works for legacy JSON with UTF-16 or UTF-32 (with/without BOM).too.
History
Date User Action Args
2020-07-13 13:01:41methanesetnosy: + methane
messages: + msg373602
2020-07-12 15:52:30serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg373559

resolution: rejected
stage: patch review -> resolved
2020-07-12 14:35:29xtreaksetnosy: + rhettinger, ezio.melotti, xtreak
messages: + msg373555
2020-07-12 13:28:50remi.lapeyresetnosy: + remi.lapeyre
messages: + msg373553
2020-07-12 13:25:31ys19991setkeywords: + patch
stage: patch review
pull_requests: + pull_request20601
2020-07-12 13:20:43ys19991create