FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-31553: add --json-lines option to json.tool by hongweipeng · Pull Request #10051 · python/cpython · GitHub

Repository navigation

bpo-31553: add --json-lines option to json.tool - #10051

Merged
serhiy-storchaka merged 4 commits into
python:masterfrom
hongweipeng:jsonlines-for-json.tool
Nov 7, 2018
Merged

serhiy-storchaka merged 4 commits into
python:masterfrom
hongweipeng:jsonlines-for-json.tool

Conversation

hongweipeng commented Oct 23, 2018 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The change LGTM, but I'm not 100% sure about the usefulness of the feature :-) See discussion at https://bugs.python.org/issue31553

Comment thread Lib/json/tool.py Outdated
try:
obj = json.load(infile)
if jsonlines:
objs = [json.loads(line) for line in infile]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Instead of loading all lines and only later dump all objects, it may be better to: load on object, dump it, then load the second object, etc.

The behavior difference is that first JSONs are written into outfile if they are valid, but if the last one is invalid. With the current code, it's all-or-nothing. Maybe it's done on purpose?

The other advantage of working on a single object is to reduce the memory footprint when working with large JSON files.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good idea.

Just make objs an iterator:

objs = (json.loads(line) for line in infile)

or

objs = map(json.loads, infile)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Wow, that's great. Thank you for your guidance.

Comment thread Lib/json/tool.py Outdated
help='write the output of infile to outfile')
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
parser.add_argument('--jsonlines', action='store_true', default=False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I suggest to name the option --json-lines or just --lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

--json-lines named similar to the existing --sort-keys,it will be more suitable.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Needed the documentation and an entry in What's New.

Comment thread Lib/json/tool.py
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
parser.add_argument('--jsonlines', action='store_true', default=False,
help='parse input using the jsonlines format')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe 'parse every input line as separate JSON object'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Write whatsnew in 3.8?

Comment thread Lib/json/tool.py Outdated
try:
obj = json.load(infile)
if jsonlines:
objs = [json.loads(line) for line in infile]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good idea.

Just make objs an iterator:

objs = (json.loads(line) for line in infile)

or

objs = map(json.loads, infile)

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment thread Doc/whatsnew/3.8.rst Outdated


json.tool
-------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add an empty line after "---". And the number of minuses should match the length of the header.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I have limited knowledge about Doc.Thank you for your patience.

Comment thread Doc/whatsnew/3.8.rst Outdated

json.tool
-------
Add option ``--json-lines`` to parse every input line as separate JSON object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add "(Contributed by ... in :issue:`...`.)"

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

Copy link
Copy Markdown

Thanks for making the requested changes!

@serhiy-storchaka: please review the changes made to this pull request.

hongweipeng changed the title bpo-31553: add jsonlines option to json.tool bpo-31553: add --json-lines option to json.tool Oct 26, 2018

matrixise left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You can add your full name if you prefer.

Copy link
Copy Markdown
Contributor Author

@matrixise It's my full name :)

Copy link
Copy Markdown
Contributor Author

ping

Copy link
Copy Markdown

@serhiy-storchaka: Please replace # with GH- in the commit message next time. Thanks!

Copy link
Copy Markdown
Member

Damn it! I have pressed Enter by mistake while edited commit message.

Thank you for your contribution @hongweipeng!

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka Oh!It is a beautiful mistake for me. Will you revoke this merger?

Copy link
Copy Markdown
Member

It can't be revoked without making larger harm.

hongweipeng deleted the jsonlines-for-json.tool branch March 16, 2020 03:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL