how can i process ndjson by rxjs(Angular)?

93 views Asked by At

For example, I can send request to openai-api by http-client

import { HttpClient } from '@angular/common/http';

return this.http.post(open-ai-interface).subscribe(...);

But, this interface returns ndjson, not json, so, how can I process this? I want a typewriter effect

I have tried ndjson, but I don't know how to use it, or there's having a better choice?

1

There are 1 answers

3
heaxyh On

you can do this simple trick :

const json = "[" + ndjson.replace(/\r?\n/g, ",").replace(/,\s*$/, "") + "]";
const jsondata = JSON.parse(json);