expressjs / expressjs/body-parser

support for ndjson

Open
#478 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.5k
Forks
767
Avg merge
13h 53m
Merged PRs (30d)
10

Description

It would be nice if the `json` middleware could support [ndjson](http://ndjson.org/) as well.
e.g, something like this as input:
```
{ "id": 1, "message": "hello" }
{ "id": 2, "message": "I'm a JSON inside a NDJSON" }
```

I've recently tried to consume a webhook of some big SaaS company, and they call the webhook with a POST and `Content-Type: application/json` (they should be passing `Content-Type: application/x-ndjson`).

From my perspective -
I didn't even know that ndjson exist until yesterday, and it's so similar to json - I would've hoped anything that can consume json can also consume ndjson. It's very easy to make that work.

What I had in mind -
add an option, something like `allow_ndjson` (default to `false` for backward compatibility), and if it's `true` -
when parsing a JSON fails, try splitting the body by `\n`, and parsing each part as json. return an array of the resulting jsons.
So the example above would result it:
```
[
{ "id": 1, "message": "hello" },
{ "id": 2, "message": "I'm a JSON inside a NDJSON" }
]
```

I'll be happy to contribute PR if there's a chance it would be approved.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.