Thread overview
Converting JSONValue to AssociativeArray.
Aug 01, 2022
hype_editor
Aug 01, 2022
Ali Çehreli
Aug 02, 2022
hype_editor
August 01, 2022

I need to convert variable of type JSONValue to variable of type string[string] (AssociativeArray).

import std.json : JSONValue;
import std.stdio : writefln;

void main()
{
    JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`);
    writefln("%s", data);

    auto aa_data = /+ Converting +/;
    writefln("%s", aa_data);
}

And output will be:

["name":"Hype Editor","hobby":"Programming"]

How can I do this in D?

August 01, 2022

On 8/1/22 2:00 PM, hype_editor wrote:

>

I need to convert variable of type JSONValue to variable of type string[string] (AssociativeArray).

import std.json : JSONValue;
import std.stdio : writefln;

void main()
{
     JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`);
     writefln("%s", data);

     auto aa_data = /+ Converting +/;
     writefln("%s", aa_data);
}

And output will be:

["name":"Hype Editor","hobby":"Programming"]

How can I do this in D?

// option 1
    string[string] aa_data;
    foreach(string k, v; data) {
        aa_data[k] = v.get!string;
    }
// option 2
    import std.algorithm : map;
    import std.array : assocArray;
    import std.typecons : tuple;
    auto aa_data = data.object
        .byKeyValue
        .map!(kv => tuple(kv.key, kv.value.get!string))
        .assocArray;

I personally prefer the straightforward loop.

-Steve

August 01, 2022
On 8/1/22 15:47, Steven Schveighoffer wrote:

You beat me to it. I used .object and .str:

import std;

void main()
{
    JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`);
    writefln("%s", data);

    // This already sees the data as an AA but the type is JSONValue[string]":
    auto o = data.object;
    static assert(is(typeof(o) == JSONValue[string]));

    writeln("Accessing the data with .str:");
    foreach (key, jsonValue; o) {
      writeln("  ", key, ": ", jsonValue.str);
    }

    // If that much is not sufficient, you can convert it to a
    // proper AA like this:
    auto aa_data = o
                   .byKeyValue
                   .map!(kv => tuple(kv.key, kv.value.str))
                   .assocArray;
    static assert(is(typeof(aa_data) == string[string]));

    writeln("Now it's string[string]:");
    writefln("  %s", aa_data);
}

Ali

August 02, 2022

On Monday, 1 August 2022 at 22:47:03 UTC, Steven Schveighoffer wrote:

>

On 8/1/22 2:00 PM, hype_editor wrote:

>

[...]

// option 1
    string[string] aa_data;
    foreach(string k, v; data) {
        aa_data[k] = v.get!string;
    }
// option 2
    import std.algorithm : map;
    import std.array : assocArray;
    import std.typecons : tuple;
    auto aa_data = data.object
        .byKeyValue
        .map!(kv => tuple(kv.key, kv.value.get!string))
        .assocArray;

I personally prefer the straightforward loop.

-Steve

Steve, thank you very much, works fine!