April 05, 2015 Vibe.d json to csv | ||||
|---|---|---|---|---|
| ||||
So I am using vibe.d json module to parse some array data with homogeneous objects, and I want to convert it to CSV.
Aside from encoding double-qoutes, this is want I came up with to generate the header:
```
void csvHeader(const Json jsonObject)
{
return keys(jsonObject.get!(Json[string])).stdMap!(a =>`"`~a~`"`).joiner(",");
}
```
I can probably use zip to get the quotes around the names, to avoid concatenation. But it works fine the way it is. The problem is, the following doesn't:
```
void csvRow(const Json jsonObject)
{
return values(jsonObject.get!(Json[string])).stdMap!(a =>`"`~a~`"`).joiner(",");
}
```
source\app.d(84): Error: template std.algorithm.joiner cannot deduce function from argument types !()(MapResult!(__lambda3, const(Json)[]), string), candidates are:
std\algorithm.d(3403): std.algorithm.joiner(RoR, Separator)(RoR r, Separator sep) if (isInputRange!RoR && isInputRange!(ElementType!RoR) && isForwardRange!Separator && is(ElementType!Separator : ElementType!(ElementType!RoR)))
std\algorithm.d(3670): std.algorithm.joiner(RoR)(RoR r) if (isInputRange!RoR && isInputRange!(ElementType!RoR))
| ||||
April 07, 2015 Re: Vibe.d json to csv | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sebastiaan Koppe | Am 05.04.2015 um 06:24 schrieb Sebastiaan Koppe:
> (...)
> I can probably use zip to get the quotes around the names, to avoid
> concatenation. But it works fine the way it is. The problem is, the
> following doesn't:
>
> ```
> void csvRow(const Json jsonObject)
> {
> return values(jsonObject.get!(Json[string])).stdMap!(a
> =>`"`~a~`"`).joiner(",");
> }
> ```
From the looks of it, the problem is that the concatenation there yields a Json value instead of a string (because concatenation also works for adding elements to a JSON array for example). If all elements are known to be strings, using `"`~a.get!string~`"` should make it work.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply