How to make one json from two tables?

Posted by goo on 06-Feb-2017 09:39

I would like to make a json from a temp-table that combines fields for two or more tables. I know I can do this dynamicly but wonder if anyone have another way of accomplish this.

i.e. SportsDB with Customer and Salesrep.

I would like to use the tth:write-json(). What I am thinking of now is to walk through the two tables Customer and Salesrep, and add all fields from first table, and add one from the salesrep by making a function that gives the two tables + an extra parameter for the fields that should be added, or just add all fields (exept the one that allready is in customer (salesrep field).

Is there an easier way of doing this? 

Posted by Peter Judge on 06-Feb-2017 10:50

Something like thebelow should work.
 
Def var jsonData as JsonObject.
Def var jsonParent as JsonData.
 
jsonData = new JsonOject().
jsonData:Read(hdlTempTable).
 
jsonParent:Add(‘Customer’, jsonData).
 
 
 
 

All Replies

Posted by Peter Judge on 06-Feb-2017 09:44

Look at the Json Object’s Read() method … you can pass buffer handles and the temp-table JSON is added to the JSON object
 
 

Posted by goo on 06-Feb-2017 10:09

Thanks for opening my eyes :-) I just did a myJson:Read(bhCustomer). myJson:Add('Salesrep',bhSalesrep::Salesrep), and voila, I got it...

Thanks !!

Posted by goo on 06-Feb-2017 10:38

Peter, after reading I am kind of strugling to understand how to add a "Customer": in front of the JsonArray:write(). I am not able to see any way of adding that... feels a bit stupied :-)

Posted by Peter Judge on 06-Feb-2017 10:50

Something like thebelow should work.
 
Def var jsonData as JsonObject.
Def var jsonParent as JsonData.
 
jsonData = new JsonOject().
jsonData:Read(hdlTempTable).
 
jsonParent:Add(‘Customer’, jsonData).
 
 
 

Posted by goo on 06-Feb-2017 11:07

aah :-) thanks... thats the way

Posted by goo on 06-Feb-2017 12:29

Ok Peter, I give up.... I thought I had a solution, but I don't understand why the following happens...

for each wdProcess, first wdProcessType of wdProcess:

 oObject:read(buffer wdProcess:handle).

 oObject:add("ProcessTypeName",buffer wdProcessType::ProcessTypeName).

 oArray:add(oObject).

end.

oHeader:Add('Process',oArray).

oHeader:write(myLC,true).

message string(myLC) view-as alert-box.

I get a lot of records, but they all are the same value as the last record. I have tried to add with index, but no go. What am I doing wrong?

Posted by goo on 06-Feb-2017 12:37

And then I found it :-)) Of course I was overwriting the same oObject ... when I added oObject = NEW oObject(). I got it to work ....

This thread is closed