Skip to content

bpo-31553: Add --jsonlines option to json.tool.#3846

Closed
jeffreyrack wants to merge 10 commits intopython:masterfrom
jeffreyrack:bpo31553
Closed

bpo-31553: Add --jsonlines option to json.tool.#3846
jeffreyrack wants to merge 10 commits intopython:masterfrom
jeffreyrack:bpo31553

Conversation

@jeffreyrack
Copy link
Copy Markdown
Contributor

@jeffreyrack jeffreyrack commented Oct 1, 2017

Add the --jsonlines option to the json.tool utility in order to parse input that is passed using the jsonlines formatting.

https://bugs.python.org/issue31553

https://bugs.python.org/issue31553

Lib/json/tool.py Outdated
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 output using the jsonlines format.')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think there are three typos here: Parse output -> parse input (the other help texts are not capitalized) and the other texts don't end in a period.

import json
import sys

def parse_json_from_filehandle(infile, sort_keys):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These functions are a bit repetitive and add a lot of unnecessary lines of code. I think they could be avoided by keeping the functionality in the original location. The code could become more like:

sort_keys = options.sort_keys
jsonlines = options.jsonlines
with infile:
    if jsonlines:
        if sort_keys:
            objs = [json.loads(line) for line in infile]
        else:
           ...
    else:
        if sort_keys:
            objs = [json.load(infile)]
        else:
            ...

This keeps the code more concise.

@jeffreyrack jeffreyrack deleted the bpo31553 branch March 19, 2018 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants