How to read two or more levels Json and save to array

How to read two or more levels Json and save to array

Postby richard-service » Thu Aug 01, 2024 9:55 am

Hi

Harbour/xharbour Any functions to read more levels Json and save to array.

Code: Select all  Expand view

{
 "rtnCode": "00",
 "sub": [
 {
 "oType": "08",
 "rtnNum": "2",
 "sub": [
         {
         "oOrder": "BC26000245",
         "sub": [
                 {
                 "ddiOrder": "BC22932100",
                 "hosPsub": [
                             {
                             "hospName": "中華藥局",
                             "funcDT": "1130501"
                             },
                             {
                             "hospName": "中華藥局",
                             "funcDT": "1130601"
                             }
                             ],
                 "ddIsub": [
                            {
                            "ingName": "Omeprazole",
                            "ddiIngName": "Clopidogrel",
                            "effect": "增加冠心症病人心血管栓塞風險。",
                            "mechanism": "抑制 clopidogrel 經 CYP2C19 轉化為活性代謝物。",
                            "management": "避免併用;如需併用氫離子幫浦抑制劑,考慮rabeprazole (間隔 4 小時)、pantoprazol",
                            "alternatives": "無建議"
                            }
                           ]
                 }
                ]
         },
 {
 "oOrder": "BC26000245",
 "sub": [
         {
           "ddiOrder": "BC25071100",
           "hosPsub": [
                      {
                        "hospName": "大林慈濟醫",
                        "funcDT": "1130524"
                      },
                      {
                        "hospName": "大林慈濟醫",
                        "funcDT": "1130624"
                      }
                      ],
            "ddIsub": [
                      {
                        "ingName": "Omeprazole",
                        "ddiIngName": "Erlotinib",
                        "effect": "降低 erlotinib 曝藥量。",
                        "mechanism": "erlotinib 溶解度隨 pH 值增加而降低,進而減少吸收。",
                        "management": "避免併用。必要時改用制酸劑,但與 erlotinib 給藥間隔數小時。",
                        "alternatives": "無建議"
                      }
                      ]
 }
 ]
 }
 ]
 }
 ]
}
 
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 803
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: How to read two or more levels Json and save to array

Postby nageswaragunupudi » Thu Aug 01, 2024 1:42 pm

step 1: Convert to Hash ( hb_jsonDecode( cJson, @hHash ) )
step 2: Write a small function to convert hash to array
Regards

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

Re: How to read two or more levels Json and save to array

Postby richard-service » Thu Aug 01, 2024 5:42 pm

nageswaragunupudi wrote:step 1: Convert to Hash ( hb_jsonDecode( cJson, @hHash ) )
step 2: Write a small function to convert hash to array


Dear Rao,

I'll try it again. Thanks a lot.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 803
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: How to read two or more levels Json and save to array

Postby richard-service » Fri Aug 02, 2024 4:21 am

nageswaragunupudi wrote:step 1: Convert to Hash ( hb_jsonDecode( cJson, @hHash ) )
step 2: Write a small function to convert hash to array


I found FWH forums below:
https://forums.fivetechsupport.com/viewtopic.php?f=3&t=44682&hilit=hb_jsonDecode&sid=d9f385150e611269b6126fd5d63abb3e

Marc Venken Share his source code.

Code: Select all  Expand view

  hb_JsonDecode( uResponse, @hDatos )     // -> Hash
  xbrowser ( hDatos )TITLE "R Click" setup ( oBrw:bRClicked := { |r,c,f,oBrw| aResult := XbrToArray( oBrw ) } )

function XbrToArray( Self, aCols )

   local aData    := {}
   local nRows    := ::nLen
   local nRow, bm

   if nRows > 0
      if aCols == nil
         aCols    := ::GetVisibleCols()
      else
         aCols    := { |o,i| aCols[ i ] := ::oCol( i ) }
      endif

      aData       := Array( nRows, Len( aCols ) )

      //  Zet de headers mee in de array op lijn 1
      for i = 1 to len(aCols)
        aData[1][I] = ::cHeaders[I]
      next

      xbrowser(aData) title "Adata Test"

      bm          := ::BookMark

      Eval( ::bGoTop, Self )

      for nRow := 2 to nRows
         AEval( aCols, { |o,i| aData[ nRow, i ] := o:Value } )
         Eval( ::bSkip, 1 )
      next

      ::BookMark  := bm

   endif
   xbrowser(aData) COLUMNS 1,3 SETUP ( oBrw:cHeaders := {"ID","Naam"}) Title "aData extracted"

return aData

 


How to read an array of arrays within an array?
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 803
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: How to read two or more levels Json and save to array

Postby nageswaragunupudi » Fri Aug 02, 2024 2:02 pm

The above code is just a repetition of the
METHOD toArray() of XBrowse.
Regards

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

Re: How to read two or more levels Json and save to array

Postby Marc Venken » Fri Aug 02, 2024 3:17 pm

I'm also looking for the same. Data from a array in a array and this can go more levels deep.

I haven't seen a sample where Xbrowser who can show multi levels, export to Array/Dbf in a multi array way.

With

XBROWSER HashTree( hDatos ) TITLE "HASH-TREE"

We see the values inside a xbrowser, but from that moment I don't see a option to again use a RClick or Setup for the second browse that is called from the first. Therefore I think we need a
function to generated the arrays.

I my sample I try to get all the color IDs and names from my shop, what is not that deep of level

[
{
"id": 1,
"language_id": 4,
"name": "Kleur",
"sort_order": 127,
"dropdown": 0,
"origin": "",
"values":
[
{
"id": 3824,
"language_id": 4,
"name": "MELON ORANGE",
"sort_order": -1,
"hex": "f6aa70"
},
{
"id": 3346,
"language_id": 4,
"name": "",
"sort_order": 0,
"hex": ""
},
{
"id": 3514,
"language_id": 4,
"name": "",
"sort_order": 0,
"hex": ""
},
etc..
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: How to read two or more levels Json and save to array

Postby Marc Venken » Fri Aug 02, 2024 3:22 pm

I know that the hashes will contain de data and that we can adress them like :

webshop->promonew = val(hDatos["prices","specialPrice"])

But then again, i was not able to generate a loop (do while, FOR) to process a Hash )))))
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: How to read two or more levels Json and save to array

Postby nageswaragunupudi » Fri Aug 02, 2024 3:48 pm

Please give your full Json here.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
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 73 guests

cron