Page 1 of 1

Does anyone have a json reader

PostPosted: Tue Nov 08, 2022 2:59 pm
by Rick Lipkin
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

Re: Does anyone have a json reader

PostPosted: Tue Nov 08, 2022 4:03 pm
by Enrico Maria Giordano
Try HB_JSONDECODE(), it produces an hash that can then be navigated.

Re: Does anyone have a json reader

PostPosted: Tue Nov 08, 2022 6:07 pm
by leandro
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

Re: Does anyone have a json reader

PostPosted: Wed Nov 09, 2022 11:37 am
by Marc Venken
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

 

Re: Does anyone have a json reader

PostPosted: Wed Nov 09, 2022 11:48 am
by Marc Venken
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

Re: Does anyone have a json reader

PostPosted: Wed Nov 09, 2022 6:12 pm
by TimStone
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.

Re: Does anyone have a json reader

PostPosted: Thu Nov 10, 2022 3:28 am
by nageswaragunupudi
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.

Re: Does anyone have a json reader

PostPosted: Thu Nov 10, 2022 3:42 am
by nageswaragunupudi
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