Thread overview
std.net.curl get json_encode
Oct 02, 2020
Vino
Oct 02, 2020
Imperatorn
Oct 02, 2020
Imperatorn
Oct 09, 2020
Vino
Oct 09, 2020
ikod
Oct 09, 2020
Vino
Oct 09, 2020
Andre Pany
Oct 11, 2020
Vino
Oct 11, 2020
Andre Pany
October 02, 2020
Hi All,

   Request your help, the below code is working but we need the output as a json array, in PHP we have json_encode(content), so how to do the same in D, the output is as below, as we need to store this output into database table which contains columns' (Id, Hostname, pool,email_id,username)

*******************
PHP
$jdata = json_encode(content);
foreach($jdata as $j) {
print_r($j["items"]["hostname"]);
*****************

OUTPUT:
{"items":
	[
		{"id":"1",
		"hostname":"server1",
		"pool":"dev",
		"options":[
			{"optionValue":"test1@mail.com,test1"},
			{"optionValue":"123"}
			]
		},
		{"id":"2",
		"hostname":"server2",
		"pool":"dev",
		"options":[
			{"optionValue":"test2@mail.com,test2"},
			{"optionValue":"124"}
			]
		}
	]
}

import std.net.curl, std.conv, std.stdio, std.json;

void main() {
auto https = HTTP();
https.handle.set(CurlOption.userpwd, "user:pass");
https.handle.set(CurlOption.connecttimeout, 600);
https.handle.set(CurlOption.tcp_nodelay, 1);
https.handle.set(CurlOption.buffersize, 1073741824);
https.handle.set(CurlOption.http_version, 2);
https.handle.set(CurlOption.sslversion,  1);
https.handle.set(CurlOption.use_ssl,  3);
https.handle.set(CurlOption.ssl_verifypeer, 0);
https.handle.set(CurlOption.url, "https://test.com/userlist");
https.method(HTTP.Method.get);
https.StatusLine status;
https.onReceiveStatusLine = (https.StatusLine s) { status = s; };
auto content = https.perform();
https.shutdown;
writeln(to!string(content));
}

From,
Vino.B
October 02, 2020
On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
> Hi All,
>
>    Request your help, the below code is working but we need the output as a json array, in PHP we have json_encode(content), so how to do the same in D, the output is as below, as we need to store this output into database table which contains columns' (Id, Hostname, pool,email_id,username)
>
> [...]

JSONValue jv = parseJSON(content);
October 02, 2020
On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
> Hi All,
>
>    Request your help, the below code is working but we need the output as a json array, in PHP we have json_encode(content), so how to do the same in D, the output is as below, as we need to store this output into database table which contains columns' (Id, Hostname, pool,email_id,username)
>
> [...]

I also suggest you take a look at other "json packages" as well:
https://code.dlang.org/search?q=json

For example std_data_json or asdf
October 09, 2020
On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:
> On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
>> Hi All,
>>
>>    Request your help, the below code is working but we need the output as a json array, in PHP we have json_encode(content), so how to do the same in D, the output is as below, as we need to store this output into database table which contains columns' (Id, Hostname, pool,email_id,username)
>>
>> [...]
>
> I also suggest you take a look at other "json packages" as well:
> https://code.dlang.org/search?q=json
>
> For example std_data_json or asdf

Hi,

 I tired sdt_data_json, still no luck, the output of the url is as below

Output : {"Name":"testuser","Site":"1-east"}

Error :
test.d(20): Error: template `stdx.data.json.parser.parseJSONValue` cannot deduce function from argument types `!()(int)`, candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(133):        `parseJSONValue(LexOptions options = LexOptions.init, Input)(ref Input input, string filename = "", int maxDepth = defaultMaxDepth)`
  with `options = cast(LexOptions)0,
       Input = int`
  whose parameters have the following constraints:
  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
`  > isInputRange!Input
      - isSomeChar!(ElementType!Input)
        or:
      - isIntegral!(ElementType!Input)
`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(178):        `parseJSONValue(Input)(ref Input tokens, int maxDepth = defaultMaxDepth)`
  with `Input = int`
  whose parameters have the following constraints:
  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
`  > isJSONTokenInputRange!Input
`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
datacoll.d(20):        All possible candidates are marked as `deprecated` or `@disable`
  Tip: not satisfied constraints are marked with `>`

Code:
import std.net.curl, std.conv, std.stdio, stdx.data.json;


void main() {
auto https = HTTP();
https.handle.set(CurlOption.userpwd, "user:pass");
https.handle.set(CurlOption.connecttimeout, 600);
https.handle.set(CurlOption.tcp_nodelay, 1);
https.handle.set(CurlOption.buffersize, 1073741824);
https.handle.set(CurlOption.http_version, 2);
https.handle.set(CurlOption.sslversion,  1);
https.handle.set(CurlOption.use_ssl,  3);
https.handle.set(CurlOption.ssl_verifypeer, 0);
https.handle.set(CurlOption.url, "https://test.com/getUser");
https.method(HTTP.Method.get);
https.StatusLine status;
auto content = https.perform();
https.shutdown;
JSONValue jv = parseJSONValue(content);
writeln(jv["Name"]);

}

From,
Vino
October 09, 2020
On Friday, 9 October 2020 at 01:45:37 UTC, Vino wrote:
> On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:
>> On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
>>> Hi All,
...
> auto content = https.perform();
> https.shutdown;
> JSONValue jv = parseJSONValue(content);

Maybe
JSONValue jv = toJSONValue(content);


> writeln(jv["Name"]);
>
> }
>
> From,
> Vino


October 09, 2020
On Friday, 9 October 2020 at 05:30:34 UTC, ikod wrote:
> On Friday, 9 October 2020 at 01:45:37 UTC, Vino wrote:
>> On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:
>>> On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
>>>> Hi All,
> ...
>> auto content = https.perform();
>> https.shutdown;
>> JSONValue jv = parseJSONValue(content);
>
> Maybe
> JSONValue jv = toJSONValue(content);
>
>
>> writeln(jv["Name"]);
>>
>> }
>>
>> From,
>> Vino

Hi Ikod,

  No luck.

Error
test.d(19): Error: template `stdx.data.json.parser.toJSONValue` cannot deduce function from argument types `!()(int)`, candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(58):        `toJSONValue(LexOptions options = LexOptions.init, Input)(Input input, string filename = "", int maxDepth = defaultMaxDepth)`
  with `options = cast(LexOptions)0,
       Input = int`
  whose parameters have the following constraints:
  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
`  > isInputRange!Input
      - isSomeChar!(ElementType!Input)
        or:
      - isIntegral!(ElementType!Input)
`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(65):        `toJSONValue(Input)(Input tokens, int maxDepth = defaultMaxDepth)`
  with `Input = int`
  whose parameters have the following constraints:
  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
`  > isJSONTokenInputRange!Input
`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
datacoll.d(19):        All possible candidates are marked as `deprecated` or `@disable`
  Tip: not satisfied constraints are marked with `>`

From,
Vino
October 09, 2020
On Friday, 9 October 2020 at 05:56:05 UTC, Vino wrote:
> On Friday, 9 October 2020 at 05:30:34 UTC, ikod wrote:
>> On Friday, 9 October 2020 at 01:45:37 UTC, Vino wrote:
>>> On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:
>>>> On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
>>>>> Hi All,
>> ...
>>> auto content = https.perform();
>>> https.shutdown;
>>> JSONValue jv = parseJSONValue(content);
>>
>> Maybe
>> JSONValue jv = toJSONValue(content);
>>
>>
>>> writeln(jv["Name"]);
>>>
>>> }
>>>
>>> From,
>>> Vino
>
> Hi Ikod,
>
>   No luck.
>
> Error
> test.d(19): Error: template `stdx.data.json.parser.toJSONValue` cannot deduce function from argument types `!()(int)`, candidates are:
> C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(58):        `toJSONValue(LexOptions options = LexOptions.init, Input)(Input input, string filename = "", int maxDepth = defaultMaxDepth)`
>   with `options = cast(LexOptions)0,
>        Input = int`
>   whose parameters have the following constraints:
>   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
> `  > isInputRange!Input
>       - isSomeChar!(ElementType!Input)
>         or:
>       - isIntegral!(ElementType!Input)
> `  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
> C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(65):        `toJSONValue(Input)(Input tokens, int maxDepth = defaultMaxDepth)`
>   with `Input = int`
>   whose parameters have the following constraints:
>   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
> `  > isJSONTokenInputRange!Input
> `  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
> datacoll.d(19):        All possible candidates are marked as `deprecated` or `@disable`
>   Tip: not satisfied constraints are marked with `>`
>
> From,
> Vino

(Writing from my mobile, a lot of room for improvements):

import std.net.curl, std.stdio;

// GET with custom data receivers
auto http = HTTP("dlang.org");
http.onReceiveHeader =
    (in char[] key, in char[] value) { writeln(key, ": ", value); };

ubyte[] content;
http.onReceive = (ubyte[] data) {
content ~= data;
return data.length; };
http.perform();

string s = cast(string) content;

Kind regards
Andre

October 11, 2020
On Friday, 9 October 2020 at 17:50:16 UTC, Andre Pany wrote:
> On Friday, 9 October 2020 at 05:56:05 UTC, Vino wrote:
>> On Friday, 9 October 2020 at 05:30:34 UTC, ikod wrote:
>>> On Friday, 9 October 2020 at 01:45:37 UTC, Vino wrote:
>>>> On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:
>>>>> On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:
>>>>>> Hi All,
>>> ...
>>>> auto content = https.perform();
>>>> https.shutdown;
>>>> JSONValue jv = parseJSONValue(content);
>>>
>>> Maybe
>>> JSONValue jv = toJSONValue(content);
>>>
>>>
>>>> writeln(jv["Name"]);
>>>>
>>>> }
>>>>
>>>> From,
>>>> Vino
>>
>> Hi Ikod,
>>
>>   No luck.
>>
>> Error
>> test.d(19): Error: template `stdx.data.json.parser.toJSONValue` cannot deduce function from argument types `!()(int)`, candidates are:
>> C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(58):        `toJSONValue(LexOptions options = LexOptions.init, Input)(Input input, string filename = "", int maxDepth = defaultMaxDepth)`
>>   with `options = cast(LexOptions)0,
>>        Input = int`
>>   whose parameters have the following constraints:
>>   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
>> `  > isInputRange!Input
>>       - isSomeChar!(ElementType!Input)
>>         or:
>>       - isIntegral!(ElementType!Input)
>> `  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
>> C:\D\dmd2\windows\bin\..\..\src\phobos\stdx\data\json\parser.d(65):        `toJSONValue(Input)(Input tokens, int maxDepth = defaultMaxDepth)`
>>   with `Input = int`
>>   whose parameters have the following constraints:
>>   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
>> `  > isJSONTokenInputRange!Input
>> `  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
>> datacoll.d(19):        All possible candidates are marked as `deprecated` or `@disable`
>>   Tip: not satisfied constraints are marked with `>`
>>
>> From,
>> Vino
>
> (Writing from my mobile, a lot of room for improvements):
>
> import std.net.curl, std.stdio;
>
> // GET with custom data receivers
> auto http = HTTP("dlang.org");
> http.onReceiveHeader =
>     (in char[] key, in char[] value) { writeln(key, ": ", value); };
>
> ubyte[] content;
> http.onReceive = (ubyte[] data) {
> content ~= data;
> return data.length; };
> http.perform();
>
> string s = cast(string) content;
>
> Kind regards
> Andre

Hi Andre,

  Thank you very much, now we are able to get the data as expected using jv["Name"], now when we try to print all the returned data with Key and Values as below it is thorwing an error

Error:

Error: template `object.byKeyValue` cannot deduce function from argument types `!()(JSONValue)`

Code:

string s = cast(string) content;
JSONValue jv = parseJSONValue(s);
writeln(jv["Name"];               \\ this works.
foreach (key, value; jv.byKeyValue) writefln("%s: %s", key, value);  \\ this does not work.

From,
Vino
October 11, 2020
On Sunday, 11 October 2020 at 08:48:16 UTC, Vino wrote:
> On Friday, 9 October 2020 at 17:50:16 UTC, Andre Pany wrote:
>> [...]
>
> Hi Andre,
>
>   Thank you very much, now we are able to get the data as expected using jv["Name"], now when we try to print all the returned data with Key and Values as below it is thorwing an error
>
> Error:
>
> Error: template `object.byKeyValue` cannot deduce function from argument types `!()(JSONValue)`
>
> Code:
>
> string s = cast(string) content;
> JSONValue jv = parseJSONValue(s);
> writeln(jv["Name"];               \\ this works.
> foreach (key, value; jv.byKeyValue) writefln("%s: %s", key, value);  \\ this does not work.
>
> From,
> Vino

As far as I remember:
foreach (key, value; jv.object.byKeyValue)

byKeyValue is a function of an associative array. You get the associative array (Json object) of an JSONValue by attribute .object.

Kind regards
Andre