Page 1 of 1

read JSON

Posted: Tue Nov 16, 2021 11:09 am
by Otto
Hello friends,
Do we have any FW/Harbour functions to read such a JSON data?
Thank you in advance
Otto

Code: Select all | Expand




// 'JSON' data included
data = '[{"expanded":true,"folder":true,"key":"1","title":"Item1","children":[{"key":"2","title":"Item2"},{"key":"3","title":"Item3"},{"key":"4","title":"Item4"},{"key":"5","title":"Item5"}]},{"folder":true,"key":"6","title":"Item6","children":[{"key":"7","title":"Item7"},{"key":"8","title":"Item7"},{"key":"9","title":"Item8"}]},{"expanded":true,"folder":true,"key":"_2","title":"Itemwithnorecno","children":[{"folder":true,"key":"11","title":"Item11","children":[{"key":"12","title":"Item12"},{"key":"13","title":"Item13"},{"key":"14","title":"Item14"},{"key":"15","title":"Item15"}]},{"folder":true,"key":"16","title":"Item16","children":[{"key":"17","title":"Item17"},{"key":"18","title":"Item18"},{"key":"19","title":"Item19"}]}]}]';


 

Re: read JSON

Posted: Tue Nov 16, 2021 11:31 am
by cnavarro
Dear Otto, I do not know if I understand well what you need

Code: Select all | Expand


   local hDatos    := { => }
   local data := '[{"expanded":true,"folder":true,"key":"1","title":"Item1","children":[{"key":"2","title":"Item2"},{"key":"3","title":"Item3"},{"key":"4","title":"Item4"},{"key":"5","title":"Item5"}]},{"folder":true,"key":"6","title":"Item6","children":[{"key":"7","title":"Item7"},{"key":"8","title":"Item7"},{"key":"9","title":"Item8"}]},{"expanded":true,"folder":true,"key":"_2","title":"Itemwithnorecno","children":[{"folder":true,"key":"11","title":"Item11","children":[{"key":"12","title":"Item12"},{"key":"13","title":"Item13"},{"key":"14","title":"Item14"},{"key":"15","title":"Item15"}]},{"folder":true,"key":"16","title":"Item16","children":[{"key":"17","title":"Item17"},{"key":"18","title":"Item18"},{"key":"19","title":"Item19"}]}]}]'
   hb_JsonDecode( data, @hDatos )
   XBrowse( hDatos )
 

Re: read JSON

Posted: Tue Nov 16, 2021 1:22 pm
by Otto
Dear Cristobal,

Thank you. Yes, I can see all the data. But I need the order and then I have to convert it into a DBF.

When I make changes in the tree, I get a JSON file as a response.

Best regards,
Otto

Image

Re: read JSON

Posted: Tue Nov 16, 2021 5:24 pm
by Otto
Dear Cristobal,
xBrowse handles the response perfectly. But in my case, I only have harbour - no FW.

Best regards,
Otto


Image

Re: read JSON

Posted: Tue Nov 16, 2021 10:12 pm
by Marc Venken
Otto,

I also want the Json into a Dbf and i'm very close to succeed i think. The sample will show severall stages of data, but I think that there is usefull code insite for you issues.
It's a working sample, but a error at the end that i need to look into.. Just not able to write the dbf.
The // comments is for my thinking, so not directed to you, but maybe they can help

Code: Select all | Expand


#include "FiveWin.ch"

Function Main()
   local oWnd, oActiveX1, oActiveX2, cTemp

   DEFINE WINDOW oWnd TITLE "FiveWin multiple ActiveX support"

   @ 9,5 BUTTON "&Hash"   OF oWnd SIZE 40, 20  ACTION TestHash()

   ACTIVATE WINDOW oWnd

return nil

#include "Fivewin.ch"

//----------------------------------------------------------------------------//

Function TestHash()

   local hDatos    := { => }
   local aTest    := {}
   local aResult  := {}
   local aFields  := {}
   local m

   local uResponse, cCookies, I

   uResponse := '{"uprid":"2550","productId":"2550","stockId":"-1","referenceId":"0","isBundle":"0","bundledProducts":"[]","status":"1","quantity":"0","maximumQuantity":"null","ean":"98745123456","sku":"skuData","model":"APITEST","hsCode":"","name":"Noyca","price":"0.0000","taxClassId":"3","taxRate":"21.0000","isPhysical":"1","isDigital":"0","isQuotation":"0","url":"onderhoud\/api-test","urlAbs":"https:\/\/http://www.maveco-webshop.be\/onderhoud\/api-test","attributesString":"","backOrder":0,"onHome":0,"layover":0,"configurationAllowCheckout":0,"configurationStockCheck":0,"attributes":[],"description":"Memo<b>Data<\/b>","addonInstagramEnabled":"0","addonInstagram":"0","addonOcsEnabled":"0","dateAdded":"2021-10-30 18:40:57","lastModified":"2021-10-30 19:17:39","descriptions":{"description":"Memo<b>Data<\/b>","extra":"","intro":""},"images":["5101000.jpg","","","","","","","","","","",""],"image_alt":["","","","","","","","","","","",""],"discounts":{"name":"0","groupDiscount":"0.00","groupDiscountCategory":"0.00","groupDiscountProduct":"0.00"},"category":{"id":"888982","name":"Onderhoud","categoryIds":"888982","cPath":"888982"},"quantities":{"quantity":"0","physicalQuantity":"0","minimumQuantity":"0","maximumQuantity":null,"externalQuantity":"0","quantityStep":"1","trackQuantity":"1"},"prices":{"basePrice":"0.0000","normalPrice":"0.0000","price":"0.0000","specialPrice":"0.0000","purchasePrice":"0.0000"},"manufacturer":{"id":0,"name":"","image":""},"dimensions":{"type":"0","width":0,"height":0,"area":0,"weight":0},"reviews":{"count":0,"avg":0},"resources":[],"addons":{"multicatalog":{"enabled":false},"layover":{"enabled":true}}}'

   hb_JsonDecode( uResponse, @hDatos )     // -> Hash
   XBrowse( hb_hKeys( hDatos ) )           // -> Array
   XBrowse( hb_hValues( hDatos ) )         // -> Array
   XBrowse( hDatos )

   // simple key
   cCookies := If( hb_HHasKey( hDatos, "name" ), hb_hGet( hDatos, "name" ), "Not Found" )
   msginfo(cCookies) // Found

   // complex key a array insite a field //  "prices":{"basePrice":"0.0000","normalPrice":"0.0000","price":"0.0000","specialPrice":"0.0000","purchasePrice":"0.0000"}
   // Extract 1 complex key if the keyname is known
   if hb_HHasKey( hDatos, "prices" )
     msginfo("Found")
     XBrowse( hb_hKeys( hDatos["prices"] ) )           // is showing the items insite the sub array
     XBrowse( hb_hValues (hDatos["prices"] ) )         // -> Array
     XBrowse( hDatos["prices"] )         // Items and Values subarray
     //  At this point, I could stuff the data insite a dbf.  I have fields and values
   else
     msginfo("Not Found")
   endif

   // extract all keys, not knowing if there are subarray (keys)
   aTest = hb_hKeys( hDatos )           // -> Array
   xbrowse(aTest)

   for i = 1 to len(aTest)
     cZoek = aTest[i]
     msginfo(cZoek)
     if hb_HHasKey( hDatos, cZoek )
       aResult = hDatos[cZoek]          // -> Array
       XBrowse( aResult )           // is showing the items, also the subitems

       //  Here I want to build a array (aFields) with all the keys that are seen, with there values.
       //  not working

       for m = 1 to len(aResult)
         aadd(aFields,aResult)
       next m

       //XBrowse( aResult )           // is showing the items insite the sub array
     endif
     //XBrowse( aFields )           // is showing the items insite the sub array
     // Build the dbf with the array.

     // Maybe better to build a single Hash with all fields.  Than a hashtodbf save can be done.

   next



Return nil

 

Re: read JSON

Posted: Tue Nov 16, 2021 10:21 pm
by Marc Venken
Functions that can help are here...


http://www.fivetechsoft.com/harbour-docs/api.html

Re: read JSON

Posted: Wed Nov 17, 2021 7:53 am
by Otto
Hello Mark,
thank you for your help.
Yes, with xBrowse I can read the JSON file perfectly.

But in my case, I cannot use xBrowse because it is a pure HARBOR application and therefore the FIVEWIN components cannot be used.

But I've come a little further. I lack the knowledge about trees. I think I can do it for my application. It is then not universally applicable, but good for my purpose.
Best regards,
Otto



Image

Re: read JSON

Posted: Wed Nov 17, 2021 5:53 pm
by Otto
Hello friends,
Correctly resolving this JSON file is not that easy.
But with the HARBOURINO preprocessor, I brought the source code into a clear and maintainable form.

HARBOURINO preprocessor is a game changer.

Best regards,
Otto


Image

Re: read JSON

Posted: Mon Dec 06, 2021 9:33 pm
by Otto
Hello friends,
Now I am very close that all the changes inside the tree are saved in a DBF file.
This DMS is becoming a great tool.
Does someone use Help&Manual. In practice, this is doing the same.
But you have the advantage that the text is stored in HTML and not in a proprietary format. And that you can extend as you like.
Best regards,
Otto

Image

Image