Welcome! Log In Create A New Profile

Advanced

[WD 22] Create nested json

Posted by Mauricio 
[WD 22] Create nested json
May 05, 2019 08:13AM
Hi guys,
I need to create a Json file like this one:
{
"CustNumber":1001,
"CustName":"Test Customer 1",
"ContactDetail":
[
{
"CustContactGuiID":"9044F853-9F90-4EA2-9CBF-A75EC9F9BBF1",
"CustContactLastName":"Smith",
"CustContactName":"John",
"CustContactUserName":"johnsmith@gmail.com",
"CustContactPassword":"0E 03 83 21 51 3C 53 89 EC 7C 23 01 1F 6A 08 93\r\n3",
"CustContactLastUpdate":"2019-05-04T19:31:30.403",
"CustNumber":1001
}
],
"CustNumber":1003,
"CustName":"Test Customer 2",
"ContactDetail":
[
{
"CustContactGuiID":"0657E5DF-211E-416B-819D-DB82332D4C6E",
"CustContactLastName":"Doe",
"CustContactName":"Jane",
"CustContactUserName":"janedoe@mail.com",
"CustContactPassword":"0E 03 83 21 51 3C 53 89 EC 7C 23 01 1F 6A 08 93\r\n3",
"CustContactLastUpdate":"2019-05-04T19:31:30.403",
"CustNumber":1003
}
]
}

Data is coming from a query, I know how to format a string but I wonder if there is a better way to do it.
Thanks for any help.
Re: [WD 22] Create nested json
May 05, 2019 03:57PM
Hello Mauricio,

Use a variant variable then in the end use VariantToJSON.

nJsonOut is Variant
HExecuteQuery(QRY_JSON_Header)
IF HReadFirst(QRY_JSON_Header) THEN
	//Set error code and error message		
	nJsonOut.ErrorCode = "N"
	nJsonOut.ErrorMessage = "Success"

//This would be first level of the JSON
nRecs is int = HNbRec(QRY_JSON_Header)
FOR i = 1 TO nRecs
	nJsonOut.Orders.Header.PO_Number = QRY_JSON_Header.PONumber
	nJsonOut.Orders.Header.OrderNo = QRY_JSON_Header.SONumber
	nJsonOut.Orders.Header.SystemCode = "KCC"

      //Execute the nested query
        QRY_JSON_LineItems.P_SONumber= QRY_JSON_Header.SONumber
	HExecuteQuery(QRY_JSON_LineItems,hQueryDefault)
	nItems is int = HNbRec(QRY_JSON_LineItems)
	HReadFirst(QRY_JSON_LineItems)
     FOR n = 1 TO nItems
	nJsonOut.Orders.Header.LineItems.LineItem[n].Productcode = QRY_JSON_LineItems.ProductCode
	nJsonOut.Orders.Header.LineItems.LineItem[n].ProductDescription = QRY_JSON_LineItems.Description
	nJsonOut.Orders.Header.LineItems.LineItem[n].OrderLineNo = QRY_JSON_LineItems.OrderLineNumber
	nJsonOut.Orders.Header.LineItems.LineItem[n].OrderQty = QRY_JSON_LineItems.Qty
	nJsonOut.Orders.Header.LineItems.LineItem[n].LineType = QRY_JSON_LineItems.CabinetType
         //Read next record of Line Item query
         HReadNext(QRY_JSON_LineItems)
     END

//Read next record of the of the header
HReadNext(QRY_JSON_Header)
END

sJSONString is string = VariantToJSON(nJsonOut,psdFormatting)

sPath is string = CompleteDir(fExeDir())

fSaveText("JSONFile.txt",sJSONString)


Dennis W
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: