FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to read two or more levels Json and save to array
Posts: 866
Joined: Tue Oct 16, 2007 08:57 AM

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

Posted: Thu Aug 01, 2024 09:55 AM
Hi

Harbour/xharbour Any functions to read more levels Json and save to array.
Code (fw): Select all Collapse
{
 "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 (r2503251254) => Borland C++ v7.7 32bit

MySQL v8.0

Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 64bit
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

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

Posted: Thu Aug 01, 2024 01: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
Posts: 866
Joined: Tue Oct 16, 2007 08:57 AM

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

Posted: Thu Aug 01, 2024 05: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 (r2503251254) => Borland C++ v7.7 32bit

MySQL v8.0

Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 64bit
Posts: 866
Joined: Tue Oct 16, 2007 08:57 AM

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

Posted: Fri Aug 02, 2024 04: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 (fw): Select all Collapse
  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 (r2503251254) => Borland C++ v7.7 32bit

MySQL v8.0

Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 64bit
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

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

Posted: Fri Aug 02, 2024 02:02 PM

The above code is just a repetition of the

METHOD toArray() of XBrowse.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM

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

Posted: Fri Aug 02, 2024 03: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.08 with Harbour
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM

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

Posted: Fri Aug 02, 2024 03: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.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

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

Posted: Fri Aug 02, 2024 03:48 PM

Please give your full Json here.

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion