I have a csv file and I want to conver it into a JSON.
I' m reading the CSV file with HttpClient and then I'm using csvToJson to convert it.
This test code works:
this.httpClient
.get('assets/csv/results.csv', { responseType: 'text' })
.subscribe(
(data) => {
csv()
.fromString(data)
.subscribe((jsonObj) => console.log(jsonObj));
}
);
But when I try to merge the two observables to create a function:
convert() {
this.httpClient
.get('assets/csv/results.csv', { responseType: 'text' })
.pipe(switchMap((d) => csv().fromString(d)))
.subscribe((c) => console.dir(c));
}
I get this error:
core.js:4352 ERROR TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:27)
at innerSubscribe (innerSubscribe.js:69)
at SwitchMapSubscriber._innerSub (switchMap.js:44)
at SwitchMapSubscriber._next (switchMap.js:34)
at SwitchMapSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
at MapSubscriber.next (Subscriber.js:49)
at FilterSubscriber._next (filter.js:33)
at FilterSubscriber.next (Subscriber.js:49)
at MergeMapSubscriber.notifyNext (mergeMap.js:70)
at SimpleInnerSubscriber._next (innerSubscribe.js:10)
at SimpleInnerSubscriber.next (Subscriber.js:49)
at XMLHttpRequest.onLoad (http.js:1678)
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27474)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
Update
I've created a stackblitz to experiment.
In the stackblitz I get an error I don't get on localhost:
Error in src/app/csv-2-json.service.ts (18:24)
This expression is not callable.
Type '{ default: (param?: Partial<CSVParseParam>, options?: any) => Converter; }' has no call signatures.
In the library source code I see that the fromString() returns a Converter that implements PromiseLike<any[]> so I thought it should work.
What I'm doing wrong?
FilterSubscriberwhile I do not see anyfilteroperator in the pipe. It may be possible thatfilteris called internally byhttpClinet.getbut I am not sure and therefore I wonder if the log you are showing is the right one for the example. - Picci