Help on asdf json module

Vino akashvino79 at gmail.com
Mon Oct 26 09:43:44 UTC 2020


On Sunday, 25 October 2020 at 08:22:06 UTC, 9il wrote:
> On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:
>> Hi All,
>>
>>    Currently we are testing various json module such as 
>> "std.json, std_data_json, vibe.data.json and asdf", the below 
>> code works perfectely while use  "std_data_json or 
>> vibe.data.json" but not working as expected when we use "asdf" 
>> and throwing the below error, hence request your help on the 
>> same.
>>
>> [...]
>
> Hi Vino,
>
> byElement should be used here for ASDF.
>
> foreach(j; jv["items"].byElement)
>
> http://asdf.libmir.org/asdf_asdf.html#.Asdf.byElement

Hi All,

   Thank you for your help, and now need your suggestion as the 
below code is working and it also prints an additional value 
which is not expected, so request your help on how to avoid the 
additional value,

Code:
import std.stdio: writeln;
import asdf: parseJson;
import std.conv: to;
import std.range: takeExactly;
import std.array: split;

void main()
{
  string apidata = `{"items":
   [
     {"name":"T01","hostname":"test01","pool":"Development","type":
     [{"Value":"D,dev at dev.com,DEV"},{"Value":"000"}]},
     {"name":"T02","hostname":"test02","pool":"Quality","type":
     [{"Value":"Q,qas at qas.com,QAS"},{"Value":"100"}]},
     {"name":"T03","hostname":"test03","pool":"Production","type":
     [{"Value":"P,prd at prd.com,PRD"},{"Value":"100"}]}
   ]
}`;
  auto jv = parseJson(apidata);
  foreach(j; jv["items"].byElement()){
     foreach(i; j["type"].byElement().takeExactly(1)) {
        writeln(i["Value"].get!string("default").split(",")[1]);
     }
  }
}

Output:
dev at dev.com
qas at qas.com
prd at prd.com
[]   // additional value which is not expected

From,
Vino.B



More information about the Digitalmars-d-learn mailing list