www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d json to csv

reply "Sebastiaan Koppe" <mail skoppe.eu> writes:
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))
Apr 04 2015
parent =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= <sludwig rejectedsoftware.com> writes:
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.
Apr 07 2015