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

Add new APIs to support read/write file operations for ndjson format #5099

Open
daneshk opened this issue Oct 27, 2023 · 2 comments
Open

Add new APIs to support read/write file operations for ndjson format #5099

daneshk opened this issue Oct 27, 2023 · 2 comments
Labels
module/io Status/Draft In circulation by the author for initial review and consensus-building Type/NewFeature Type/Proposal

Comments

@daneshk
Copy link
Member

daneshk commented Oct 27, 2023

Description:
The current IO file APIs support read/write JSON files which require complete json object to pass for writing and returns complete object when reading. With ndjson format, which stands for "Newline Delimited JSON,". can store multiple JSON objects in a single file, with each JSON object separated by a newline character. like,

{"name": "John", "age": 30}
{"name": "Alice", "age": 25}
{"name": "Bob", "age": 35}

The key features of NDJSON are:

  1. Each line in an NDJSON file is a valid JSON object.
  2. Lines are separated by newline characters (i.e., "\n").
  3. The format is plain text, making it human-readable.

This format is useful specially for storing log data. Each log entry can be represented as a JSON object, and the entries are separated by newlines.

Having new File IO APIs for reading/writing ndjson file is useful for dumping log records to the file and read them later as stream.

Proposed APIs

// Read NDJSON file.
public isolated function fileReadNdjson(string path) returns json[]|Error;

// Write NDJSON file.
public isolated function fileWriteNdjson(string path, json[] content, FileWriteOption option = OVERWRITE) returns
Error?;

// Read NDJSON file as stream
public isolated function fileReadNdjsonAsStream(string path) returns stream<json, Error?>|Error;

// Write NDJSON file as stream
public isolated function fileWriteNdjsonFromStream(string path, stream<json, Error?> content,
                                                FileWriteOption option = OVERWRITE) returns Error?;

Related Issues (optional):
Discord discussion: https://discord.com/channels/957996897782616114/1164531854041427980/1164531854041427980
Resources: https://docs.mulesoft.com/dataweave/2.5/dataweave-formats-ndjson
https://github.com/ndjson/ndjson-spec

@daneshk daneshk added Type/NewFeature module/io Type/Proposal Status/Draft In circulation by the author for initial review and consensus-building labels Oct 27, 2023
@sameerajayasoma
Copy link
Contributor

IMO, fileWriteLines is good enough for now. I think we should consider doing this. Should we add this capability to this library?

@hasithaa, please note this when doing the data.json package.

@daneshk
Copy link
Member Author

daneshk commented Oct 28, 2023

IMO, fileWriteLines is good enough for now. I think we should consider doing this. Should we add this capability to this library?

Yes, the fileWriteLines API works. We may consider improving the fileWriteLines API to allow passing JSON objects to support this. For now, it allows only string values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/io Status/Draft In circulation by the author for initial review and consensus-building Type/NewFeature Type/Proposal
Projects
None yet
Development

No branches or pull requests

2 participants