Json output to container

Paul Backus snarwin at gmail.com
Fri Oct 30 19:33:43 UTC 2020


On Friday, 30 October 2020 at 19:07:20 UTC, Vino wrote:
>
> Requirement: parse the json string and store the output to a 
> container.
>
> From,
> Vino.B

Here's a working version of the code from your original post:

import asdf : parseJson;
import std.algorithm;
import std.container.array;
import std.stdio : writeln;
import std.typecons : Tuple, tuple;

void main()
{
     string apidata1 = `{"items": [
         {"name":"T01","hostname":"test01","pool":"Development"},
         {"name":"T02","hostname":"test02","pool":"Quality"},
         {"name":"T03","hostname":"test03","pool":"Production"}
	]}`;

	Array!(Tuple!(string, string, string)) data =
		parseJson(apidata1)["items"]
     	    .byElement
             .map!(item => tuple(
                 item["name"].get!string("default"),
                 item["hostname"].get!string("default"),
                 item["pool"].get!string("default")
             ));

     writeln(data[]);
}


More information about the Digitalmars-d-learn mailing list