www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Json Help

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

   Request your help on the below code,I am trying to convert the 
below string to json and it always throws the error, if the below 
can be accomplished with any other json package even that is 
fine, I tired only the std.json package.

Test Program: Works
````
import std.json;
import std.stdio: writeln;

void main () {

auto sadrsJson = parseJSON(`{ "TestHost": { "Building1": { 
"RackNo": 123}}}`);
writeln(sadrsJson);

}
```
Program: Does not Work
```
import std.json;
import std.stdio;
import std.container;
import std.socket: Socket;
import std.stdio: writeln;

void main () {

string host = Socket.hostName;
string registry = "Test"

Array!string output;
Array!string errors;

output.insertBack("TESTOUTPUT");
errors.insertBack("TESTERRORS");

auto sadrsJson = parseJSON({ host: { registry: { "OUTPUT": 
output[], "ERROR": errors[]}}});
writeln(sadrsJson);

}

From,
Vino.B
Sep 10 2023
parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 10.09.23 13:06, Vino wrote:
 Hi All,
 
    Request your help on the below code,I am trying to convert the below 
 string to json and it always throws the error, if the below can be 
 accomplished with any other json package even that is fine, I tired only 
 the std.json package.
 
 Test Program: Works
 ````
 import std.json;
 import std.stdio: writeln;
 
 void main () {
 
 auto sadrsJson = parseJSON(`{ "TestHost": { "Building1": { "RackNo": 
 123}}}`);
 writeln(sadrsJson);
 
 }
 ```
 Program: Does not Work
 ```
 import std.json;
 import std.stdio;
 import std.container;
 import std.socket: Socket;
 import std.stdio: writeln;
 
 void main () {
 
 string host = Socket.hostName;
 string registry = "Test"
 
 Array!string output;
 Array!string errors;
 
 output.insertBack("TESTOUTPUT");
 errors.insertBack("TESTERRORS");
 
 auto sadrsJson = parseJSON({ host: { registry: { "OUTPUT": output[], 
 "ERROR": errors[]}}});
 writeln(sadrsJson);
 
 }
 
 From,
 Vino.B
`parseJSON` takes a string as arguments and tries to parse it as JSON. The first example works, because your argument is a string and in JSON format. The argument in the second example is not a string, and if it would be a string, its not JSON (e.g. `host:` is not `"host":`)... Kind regards, Christian
Sep 12 2023