wardi / jsonlines

Documentation for the JSON Lines text file format
http://jsonlines.org
139 stars 34 forks source link

Define standard way to convert JSONL to JSON #7

Open philipashlock opened 9 years ago

philipashlock commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 1 year ago

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.