FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Does anyone have a json reader
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Does anyone have a json reader
Posted: Tue Nov 08, 2022 02:59 PM

To All

I was given a .Json file and was hoping there is someone in the forum that may have a utility I can use to read the file and then once I can read the file .. I will need to append it to a .dbf.

thanks

Rick Lipkin

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Does anyone have a json reader
Posted: Tue Nov 08, 2022 04:03 PM

Try HB_JSONDECODE(), it produces an hash that can then be navigated.

Posts: 1818
Joined: Wed Oct 26, 2005 02:49 PM
Re: Does anyone have a json reader
Posted: Tue Nov 08, 2022 06:07 PM
Después de convertir la cadena json en hash
Code (fw): Select all Collapse
aHasRes := hash()
hb_jsondecode(cJson ,@aHasRes)
Posiblemente esto te pueda ayudar
http://forums.fivetechsupport.com/viewtopic.php?f=6&t=23671&start=0&hilit=iniciandome+hash&sid=89e5462f1eeb59e44a3e36da7cfa42d0
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Does anyone have a json reader
Posted: Wed Nov 09, 2022 11:37 AM
Maybe you can see stuff insite the working function ? Stripped the retrive API calls
Code (fw): Select all Collapse
function WebApi_Inlezen()
  local aVelden:={}, lAllexport := .F., lRemove := .f., nOfferte
  local aVeldenTarget:={}, cOff, nTel:=0, nTeldone:=0
  local nOptie:= 1, lFirst := .t., lFirstpic:=.T.,lFirstATT:=.T., lFotofile:=.T.
  local afouten := {}, aNoHex:={}
  Local aNoFoto :={}, aprijscontrole:={}
  local xValue, ikl, aTest:={}
  Local at_kleuren:={}, at_maten:={}
  Local Kleur_scheider:={"-","/"}
  local hDatos    := { => }
  local aHashes := {}

  local uResponse, cCookies, I, cLink, cUrl,oHttp, cTemp

  local lFotoresend:= .F., lReedsdata:=.f.
  local awebcol:={}, aMaten:={}, cKleuren:=""
  FIELD shopveld, offvraag
  FIELD code  // shopkleur


  //  Here was my code to retrieve the JSon Data

  // In my case I have a loop running, so the JSon is changed and read every time

  uResponse = "Your Jason Data String"
  //FW_memoEdit(uResponse)

     if at("error",uResponse) > 1 .or. empty(uResponse)  //  Track errors
        aadd(aFouten,"Artikel : "+webshop->SKU+" - "+uResponse)
        webshop->(dbskip())
        loop
     endif

     hb_JsonDecode( uResponse, @hDatos )     // -> Put into a Hash

     XBROWSER HashTree( hDatos ) TITLE "HASH-TREE" ;  //  See content with Xbrowse
      SETUP ( oBrw:aCols[ 1 ]:AddBitmap( { FWDArrow(), FWRArrow() } ),;
              obrw:autofit() )

     aHashes = hb_HKeys( hDatos )  // Hash keys into array (needed for my purpose

     //  Now you can use all items from hDatos (headers see xbrowse) and use them like below
     cOnlineCode = hDatos["uprid"] //  Uprid = productcode needed to retrieve online

     //  offer command (change dbf data based on Json data)
     nOfferte = hDatos["status"] //  Neem de juiste record
     webshop->aktief = nOfferte
     webshop->online = .t.

     //  Offerte aanvraag
     nOfferte = hDatos["isQuotation"] //  Neem de juiste record
     do case
      case nOfferte = "1"  // Json was number, I needed logic
        webshop->offvraag = .T.
      otherwise
        webshop->offvraag = .F.
     endcase
     //  Naam
     cNaam = hDatos["name"] //  Neem de juiste record
     webshop->naamshop = cNaam

     //  Price
     nPrijs = val(hDatos["price"]) //  Neem de juiste record
     webshop->prijsnew = nPrijs

     webshop->(dbskip())
  enddo
  webshop->(dbunlock())
  msginfo("De gegevens werden bijgewerkt : Aantal : "+str(nTel,5))
  xbrowser(aFouten)
return
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Does anyone have a json reader
Posted: Wed Nov 09, 2022 11:48 AM

These can be interesting :

hb_hGet = to look voor a header like "price" and return the value

cPrice = str(hb_hGet(hDatos, "price"),9,2)

Sometimes the Json has multiple arrays insite

sample Json data : prices":{"basePrice":"44.8900","normalPrice":"44.8900","price":"40.5000","specialPrice":"40.5000","purchasePrice":"0.0000"}

? hDatos[ "prices", "specialPrice" ] // is correct value 40.50

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: Does anyone have a json reader
Posted: Wed Nov 09, 2022 06:12 PM
Rick,

This is what I do:
Code (fw): Select all Collapse
   LOCAL  hInitData := { => }

   // Lets decode the response
   hb_JsonDecode( cResponse, @hInitData )
   cReply   := HB_HGET( hInitData, "sid")       // Message id
The LOCAL gives me a hash.
cResponse contains the JSON code. Using that function, the Json is transferred to the hash
"sid" is the identifier for the data I want. HB_HGET( ) searches the hash for that identifier and returns it's value.
You can use as many identifiers as you want, and as many HB_HGET's to get their values.
Saving them into a variable allows you to use the value, or you can write it to a DBF.

If you don't know the identifiers, just do an XBROWSE( ) on the hash file. The first column will be the identifier, and the second column the value. When you receive the Json responses, those identifiers will be consistent, so you can grab just the ones you want out of each reply. Of course, make sure you initialize the variables first to empty values so if nothing is returned, you won't have an error.

I hope this helps. In fact, I just did this yesterday when setting up the log system for text messages, and their replies.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Does anyone have a json reader
Posted: Thu Nov 10, 2022 03:28 AM
Browsing a nested Json using HashTree
Code (fw): Select all Collapse
   c  := '{"prices":{"basePrice":"44.8900","normalPrice":"44.8900","price":"40.5000","specialPrice":"40.5000","purchasePrice":"0.0000"}}'
   hb_jsonDecode( c, @h )
   XBROWSER HASHTREE( h, 0 ) TITLE "Hash as Tree"


Now, the function HASHTREE handles nested hashes only, but not nested hashes/arrays. We will improve this for the next version. so that any complext Json can be viewd.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Does anyone have a json reader
Posted: Thu Nov 10, 2022 03:42 AM
Json Reader:
Code (fw): Select all Collapse
   c  := '{"prices":{"basePrice":"44.8900","normalPrice":"44.8900","price":"40.5000","specialPrice":"40.5000","purchasePrice":"0.0000"}}'
   hb_jsonDecode( c, @h )
   c2 := hb_jsonEncode( h, .t. )
   FW_MEMOEDIT( c2, "JSON READER (FWH)" )
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion