www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Json output to container

reply Vino <akashvino79 gmail.com> writes:
Hi,

   Request your help on the below code

Code:

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"}
   ]
   }`;
   auto data = Array!(Tuple!(string, string, string)) 
(parseJson(apidata1)
              .filter!(a => a.(["items"].byElement))
              .map!(a => tuple(a.(["name"].get!string("default")), 
a.(["hostname"].get!string("default")), 
a.(["pool"].get!string("default")))));
   writeln(data[]);
}

From,
Vino.B
Oct 30 2020
next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:
 Hi,

   Request your help on the below code

 Code:

 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"}
   ]
   }`;
   auto data = Array!(Tuple!(string, string, string)) 
 (parseJson(apidata1)
              .filter!(a => a.(["items"].byElement))
              .map!(a => 
 tuple(a.(["name"].get!string("default")), 
 a.(["hostname"].get!string("default")), 
 a.(["pool"].get!string("default")))));
   writeln(data[]);
 }

 From,
 Vino.B
What do you want to achieve and what is the problem with the code you posted? Kind regards Andre
Oct 30 2020
parent Vino <akashvino79 gmail.com> writes:
On Friday, 30 October 2020 at 17:56:22 UTC, Andre Pany wrote:
 On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:
 Hi,

   Request your help on the below code

 Code:

 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"}
   ]
   }`;
   auto data = Array!(Tuple!(string, string, string)) 
 (parseJson(apidata1)
              .filter!(a => a.(["items"].byElement))
              .map!(a => 
 tuple(a.(["name"].get!string("default")), 
 a.(["hostname"].get!string("default")), 
 a.(["pool"].get!string("default")))));
   writeln(data[]);
 }

 From,
 Vino.B
What do you want to achieve and what is the problem with the code you posted? Kind regards Andre
Hi Andre, We wanted to store the output of the json data in array container, the main goal of this request is that we are currently planning to migrate our existing code which was written in PHP to D, and our existing code deal's with many api calls with json output, so we are evaluating the feasibility, of various json package's available in D, one of the code in our existing project is as below Example code: api1 : http://test.com/server This api1 contains data "id" and "servername" api2 : http://test.com/type This api2 contains data "servername" and "type" api3 : http://test.com/catagore This api3 contains data "type" and "category" so we fetch the data using the above api's and store the data in PHP multi dimensional array like "ID", "ServerName", "Type" and "category"(temporary storage) and then we update the information in this array into MySQL table. From, Vino.B
Oct 30 2020
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:
 Hi,

   Request your help on the below code
[...]
              .filter!(a => a.(["items"].byElement))
What exactly are you trying to accomplish with this `a.(stuff)` syntax? As I'm sure you've discovered, it is not valid D, but it appears in several places in your code.
Oct 30 2020
parent reply Vino <akashvino79 gmail.com> writes:
On Friday, 30 October 2020 at 18:41:35 UTC, Paul Backus wrote:
 On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:
 Hi,

   Request your help on the below code
[...]
              .filter!(a => a.(["items"].byElement))
What exactly are you trying to accomplish with this `a.(stuff)` syntax? As I'm sure you've discovered, it is not valid D, but it appears in several places in your code.
Hi, I was just trying the example code in the link https://code.dlang.org/packages/asdf, it is just an example, hence request your help to correct me. Requirement: parse the json string and store the output to a container. From, Vino.B
Oct 30 2020
next sibling parent Vino <akashvino79 gmail.com> writes:
On Friday, 30 October 2020 at 19:07:20 UTC, Vino wrote:
 On Friday, 30 October 2020 at 18:41:35 UTC, Paul Backus wrote:
 On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:
 Hi,

   Request your help on the below code
[...]
              .filter!(a => a.(["items"].byElement))
What exactly are you trying to accomplish with this `a.(stuff)` syntax? As I'm sure you've discovered, it is not valid D, but it appears in several places in your code.
Hi, I was just trying the example code in the link https://code.dlang.org/packages/asdf, it is just an example, hence request your help to correct me. Requirement: parse the json string and store the output to a container. From, Vino.B
Requirnment : string apidata1 = `{"items": [ {"name":"T01","hostname":"test01"}, {"name":"T02","hostname":"test02"}, {"name":"T03","hostname":"test03"} ] }`; string apidata2 = `{"items": [ {"hostname":"test01","type":"Development"}, {"hostname":"test02","type":"Quality"}, {"hostname":"test03","type":"Production"} ] }`; string apidata2 = `{"items": [ {"type":"Development","Location":"L1"}, {"type":"Quality","Location":"L2"}, {type":"Production","Location":"L3"} ] }`; 1. Parse the apidata1(name,hostname) 2. For each "hostname" get the "type" data from api2(type) 3. For each "type" get the "Location: from api3(Location) 4. And store the result in a container with data (Name,Hostname,Type,Location) From, Vino.B
Oct 30 2020
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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[]); }
Oct 30 2020
parent Vino <akashvino79 gmail.com> writes:
On Friday, 30 October 2020 at 19:33:43 UTC, Paul Backus wrote:
 On Friday, 30 October 2020 at 19:07:20 UTC, Vino wrote:
 [...]
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; [...]
Hi Paul, Thank you very much, your solution resolved most of our issues. From, Vino.B
Oct 31 2020