Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define standard way to convert JSONL to JSON #7

Open
philipashlock opened this issue Feb 24, 2015 · 4 comments
Open

Define standard way to convert JSONL to JSON #7

philipashlock opened this issue Feb 24, 2015 · 4 comments

Comments

@philipashlock
Copy link

Perhaps it would also be useful to define an alternate form of jsonlines where the file would still be valid json. I'll refer to this alternate as jsonnewlines. If you wanted to treat a jsonnewlines file as a jsonlines file, you would simply ignore the first and last line of the file as well as the comma before each line separator.

For example, this jsonlines file:

{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
{"name": "May", "wins": []}
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}

Would look like this as jsonnewlines:

{"winningHands":[
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]},
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]},
{"name": "May", "wins": []},
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}
]}

It seems like this would still let you get the benefits of jsonlines while also allowing for the possibility of treating the file as JSON in whole.

@wardi
Copy link
Owner

wardi commented Feb 24, 2015

Yes, that makes sense for files on disk. I wanted to add some canonical examples of "jsonl2json" that would look something like this.

Just wondering why you choose to have an object at the outermost level instead of just a list?

@philipashlock
Copy link
Author

The object at the outermost level was only included to demonstrate that the first and last lines could have more structure than just opening and closing brackets.

@wardi
Copy link
Owner

wardi commented Feb 24, 2015

How about this:

sed -e'1s/^/[/' -e's/$/,/' -e'$s/,$/]/'
[{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]},
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]},
{"name": "May", "wins": []},
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}]

The line numbers match this way, and converting back is even simpler:

sed -e'1s/\[//' -e's/.$//'

@sp4ce
Copy link
Collaborator

sp4ce commented Apr 3, 2023

There is JSONL on one side, and then JSON on the other. We can define a "standard" way to convert JSONL to JSON, and build tool around it, but let not mix up both and say that some JSON could be JSONL if formatted in a specific way. So I'd proposed to rename, or reclassify the issue, as defining the standard way to convert JSONL to JSON instead.

@sp4ce sp4ce changed the title Alternate format to allow the file to still be valid JSON Define standard way to convert JSONL to JSON Apr 11, 2023
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

No branches or pull requests

3 participants