Does anyone have a json reader

Does anyone have a json reader

Postby Rick Lipkin » Tue Nov 08, 2022 2: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
User avatar
Rick Lipkin
 
Posts: 2665
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Does anyone have a json reader

Postby Enrico Maria Giordano » Tue Nov 08, 2022 4:03 pm

Try HB_JSONDECODE(), it produces an hash that can then be navigated.
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Does anyone have a json reader

Postby leandro » Tue Nov 08, 2022 6:07 pm

Después de convertir la cadena json en hash
Code: Select all  Expand view

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

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1676
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Does anyone have a json reader

Postby Marc Venken » Wed Nov 09, 2022 11:37 am

Maybe you can see stuff insite the working function ? Stripped the retrive API calls

Code: Select all  Expand view

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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Does anyone have a json reader

Postby Marc Venken » 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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Does anyone have a json reader

Postby TimStone » Wed Nov 09, 2022 6:12 pm

Rick,

This is what I do:

Code: Select all  Expand view

   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
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Does anyone have a json reader

Postby nageswaragunupudi » Thu Nov 10, 2022 3:28 am

Browsing a nested Json using HashTree

Code: Select all  Expand view
  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"
 


Image

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
User avatar
nageswaragunupudi
 
Posts: 10643
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Does anyone have a json reader

Postby nageswaragunupudi » Thu Nov 10, 2022 3:42 am

Json Reader:

Code: Select all  Expand view
  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)" )
 


Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10643
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 86 guests