This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Extend json.tool to handle jsonlines (with a flag)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lisroach Nosy List: Eric Moyer, ezio.melotti, lisroach, rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-09-22 16:41 by Eric Moyer, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3846 closed jeffreyrack, 2017-10-01 15:52
PR 10051 merged hongweipeng, 2018-10-23 07:11
Messages (9)
msg302755 - (view) Author: Eric Moyer (Eric Moyer) Date: 2017-09-22 16:41
json.tool should have the ability to format jsonlines data. It is a very commonly used format in many industries. Right now when given jsonlines (for example):

    echo -e '{"ingredients":["frog", "water", "chocolate", "glucose"]} | python -m json.tool

Works. But:

    echo -e '{"ingredients":["frog", "water", "chocolate", "glucose"]}\n{"ingredients":["chocolate","steel bolts"]}' | python -m json.tool

Reports an error: "Extra data: line 2 column 1 - line 3 column 1 (char 58 - 100)"

I propose that the above behavior be retained but:

    echo -e '{"ingredients":["frog", "water", "chocolate", "glucose"]}\n{"ingredients":"chocolate","steel bolts"}' | python3.7 -m json.tool --jsonlines

Should print:
{
    "ingredients": [
        "frog",
        "water",
        "chocolate",
        "glucose"
    ]
}
{
    "ingredients": [
        "chocolate",
        "steel bolts"
    ]
}

If someone else agrees this is an appropriate enhancement, I can start work on a PR in a couple of weeks.
msg302781 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-09-23 04:55
This seems like a reasonable enhancement.
msg302783 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-23 04:58
Jsonlines is an external to JSON format. You should split the data on lines and pass every line to the JSON parser separately. The same you should do with json.tool.

$ echo -e '{"ingredients":["frog", "water", "chocolate", "glucose"]}\n{"ingredients":["chocolate","steel bolts"]}' | while read -r line; do echo -n "$line" | python -m json.tool; done
{
    "ingredients": [
        "frog",
        "water",
        "chocolate",
        "glucose"
    ]
}
{
    "ingredients": [
        "chocolate",
        "steel bolts"
    ]
}
msg302785 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2017-09-23 07:39
I agree with Raymond that this is a reasonable request.
Even if jsonlines is not part of the JSON specification, the format is quite common, and practicality beats purity (espcially considering that we are just talking about json.tool).
msg302904 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-09-25 02:01
Eric, did you want to write the PR yourself or would you like for Lisa to bring it to fruition?   If you're going to do it yourself, Lisa will serve as the primary reviewer (with either Ezio or me doing the final sign-off).
msg302953 - (view) Author: Eric Moyer (Eric Moyer) Date: 2017-09-25 12:39
I planned to write the PR myself, on the principle of "it's my itch, I
should scratch it." But if you think it is better for someone else to write
it, you know the codebase better than I, and you know how long a PR takes
to review/fix. My intention is to help. I'll do whatever is easier for you.

On Sep 24, 2017 10:01 PM, "Raymond Hettinger" <report@bugs.python.org>
wrote:

>
> Raymond Hettinger added the comment:
>
> Eric, did you want to write the PR yourself or would you like for Lisa to
> bring it to fruition?   If you're going to do it yourself, Lisa will serve
> as the primary reviewer (with either Ezio or me doing the final sign-off).
>
> ----------
> assignee:  -> lisroach
> nosy: +lisroach
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31553>
> _______________________________________
>
msg328290 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-23 07:23
The output is not a valid JSON format, and is not a valid JSON Lines format. What you are going to do with it?
msg328396 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-24 22:23
> The output is not a valid JSON format, and is not a valid JSON Lines format. What you are going to do with it?

It looks useful to me to read a nicely indented JSON in the terminal (without specifying an output file). It's more readable that compact JSON on a single line.

But should we add an option to write the output on a single line? It would be the opposite of the tool description:
"A simple command line interface for json module to validate and *pretty-print* JSON objects."
msg329411 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-07 10:09
New changeset f19447994983902aa88362d8fffe645f1ea2f2aa by Serhiy Storchaka (HongWeipeng) in branch 'master':
bpo-31553: add --json-lines option to json.tool (#10051)
https://github.com/python/cpython/commit/f19447994983902aa88362d8fffe645f1ea2f2aa
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75734
2018-11-07 10:25:30serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.8, - Python 3.7
2018-11-07 10:09:37serhiy.storchakasetmessages: + msg329411
2018-10-24 22:23:52vstinnersetnosy: + vstinner
messages: + msg328396
2018-10-23 07:23:41serhiy.storchakasetmessages: + msg328290
2018-10-23 07:11:33hongweipengsetpull_requests: + pull_request9389
2017-10-01 15:52:50jeffreyracksetkeywords: + patch
stage: patch review
pull_requests: + pull_request3828
2017-09-25 12:39:04Eric Moyersetmessages: + msg302953
2017-09-25 02:01:57rhettingersetassignee: lisroach

messages: + msg302904
nosy: + lisroach
2017-09-23 07:39:33ezio.melottisetnosy: + ezio.melotti
messages: + msg302785
2017-09-23 04:58:48serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg302783
2017-09-23 04:55:43rhettingersetnosy: + rhettinger
messages: + msg302781
2017-09-22 16:41:06Eric Moyercreate