Help Using hb_JsonEncode

1,193 views
Skip to first unread message

Milan Mehta

unread,
Nov 1, 2017, 6:58:08 AM11/1/17
to Harbour Users
I wish to generate a Json file like this. 
How can I ?

TIA
Milan


---------------------------------------
{
"fp": "072017",
"b2b": [{
"ctin": "111",
"inv": [{
"inum": "TI1002",
"idt": "24-07-2017",
}]
}]
}
---------------------------------------

Teo Fonrouge

unread,
Nov 1, 2017, 1:48:41 PM11/1/17
to Harbour Users
Hi Milan,

hb_jsonEncode( <string|numeric|logical|date|timeStamp|array|hash|nil>, [<.humanReadable.>], [<codePage>] ) -> jsonString
* requieres any of above harbour types, returns a json string

hb_jsonDecode( <jsonString>, nil, [<codePage>] ) -> Harbour value
hb_jsonDecode
( <jsonString>, @var, [<codePage>] ) ->  length of decoded <jsonString>, var will contain the resulting Harbour value
* require a properly jsonString and returns a Harbour value representing the jsonString

To properly create a jsonString which is contained in curly braces "{ ... }" you need
pass to the hb_jsonEncode() function a Harbour Hash whose items has string keys


h :={"name"=>"Milan","first"=>"Mehta","age"=>35}
hb_jsonEncode
(h,.t.)

will return:

{
 
"name":"Milan",
 
"first":"Mehta",
 
"age":35
}

( and if you hb_jsonDecode it, you'll get a Harbour Hash representation of it )

So, if you want to get:

{
"fp": "072017",
"b2b": [{
"ctin": "111",
"inv": [{
"inum": "TI1002",
"idt": "24-07-2017",
}]
}]
}

then you can do the following (two methods are used for clarity):

PROCEDURE main()

    j1()
    
    WAIT
    
    j2()
    
    WAIT
    
RETURN

PROCEDURE j1()
    LOCAL h
    
    h := { => }
    
    h[ "fp" ] := "072017"
    h[ "b2b" ] := ;
{ ;
   { ;
"ctin" => "111", ;
"inv" => ;
   { ;
{ ;
   "inum" => "TI1002", ;
   "idt" => "24-07-2017" ;
} ;
   } ;
   } ;
}
    
    ? hb_jsonEncode( h, .t. )
    
RETURN

PROCEDURE j2()
    LOCAL h
    LOCAL a1
    LOCAL a2
    
    a2 := { "inum" => "TI1002", "idt" => "24-07-2017" }
    a1 := { { "ctin" => "111", "inv" => { a2 } } }
    
    h := { => }
    
    h[ "fp" ] := "072017"
    h[ "b2b" ] := a1
    
    ? hb_jsonEncode( h, .t. )
    
RETURN


HTH,


best regards,

Teo

Milan Mehta

unread,
Nov 18, 2017, 10:07:23 AM11/18/17
to Harbour Users
It worked Great !!!!
I preferred the Second Method you suggested.

Thanks a lot again.
Milan.

Milan Mehta

unread,
Nov 18, 2017, 10:08:09 AM11/18/17
to Harbour Users

Vikram Chhajer

unread,
Jan 2, 2018, 9:11:48 AM1/2/18
to Harbour Users

HELLO
 MILLAN  MEHTA   TAJNKS IN ADVANCE

CAN U SEND ME THE PRG I M ALSO SEARCHING FOR GST JSON FILE , IT WILL BE NICE IF SEND ME THE PRG  AND YOUR CONCAT NUMBER  ON MAIL ID
MY MAIL ID IS VIKRAM...@GMAIL.COM

A Santos

unread,
Aug 8, 2022, 6:15:22 PM8/8/22
to Harbour Users
Hello!

I've been trying to use the 3rd parameter (codePage) No success yet. It accepts numbers only, otherwise RTE Error BASE/1302  Argument error: HB_JSONDECODE occurs. I need to use code page "PTISO" or "PT850"...

Any ideas?

TIA

A Santos

unread,
Aug 8, 2022, 6:21:05 PM8/8/22
to Harbour Users
Solved adding following lines:

   REQUEST HB_LANG_PT
   REQUEST HB_CODEPAGE_PTISO
   HB_CDPSELECT( "PTISO" )
.
.
.hb_jsonDecode( cRet @hRet, "PTISO" )


Thanks.
Reply all
Reply to author
Forward
0 new messages