Como convertir un Json a Hash? (SOLUCIONADO)

Como convertir un Json a Hash? (SOLUCIONADO)

Postby csincuir » Thu Oct 27, 2022 11:27 pm

Hola a todos.
Tengo este Json que recibo de un Api Rest
Code: Select all  Expand view
{"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id": "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal"" "}}


Y estoy intentando encontrar sus valores, por lo que he probado convertirlo a Hash, pero no logro obtener ningun valor.
He probado de esta forma:

Code: Select all  Expand view

TEXT INTO cJs
{"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id": "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal"" "}}
ENDTEXT

cHs:={=>}
hb_jsonDecode(cJs,@cHs)
?  cHs['order']['id']
 

Y me da: Error BASE/1132 Error de rango: acceso al array

O bien:
Code: Select all  Expand view

TEXT INTO cJs
{"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id": "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal"" "}}
ENDTEXT

cHs:={=>}
hb_jsonDecode(cJs,@cHs)
xbrowse( cHs )
 

No obtengo nigun valor con xBrowse()

Alguien me puede indicar como puedo tratar este JSon por favor?

Saludos cordiales.

Carlos
Last edited by csincuir on Fri Oct 28, 2022 12:58 pm, edited 2 times in total.
csincuir
 
Posts: 407
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala

Re: Como convertir un Json a Hash?

Postby cmsoft » Fri Oct 28, 2022 1:01 am

Carlos:
Prueba con hb_jsonDecode
viewtopic.php?f=3&t=34345#p203772
User avatar
cmsoft
 
Posts: 1290
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina

Re: Como convertir un Json a Hash?

Postby csincuir » Fri Oct 28, 2022 3:32 am

Cesar, gracias por contestar, pero, tambien ya he usado hb_jsonDecode, pero acá me convierte el Json en una cadena con slashes \\ nada mas.

Saludos
csincuir
 
Posts: 407
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala

Re: Como convertir un Json a Hash?

Postby csincuir » Fri Oct 28, 2022 3:38 am

Tal ves mi pregunta esta mal hecha.
Code: Select all  Expand view

cHs:={=>}
hb_jsonDecode(cJs,@cHs)
? ValType( cHs )
 


La función hb_jsonDecode() si me convierte el Json a Hash, ya que ValType(cHs) me devuelve "H", por lo que la pregunta sería, como hago ahora para obtener los datos del Hash generado?
ya que cHs['order']['id'], me da Error de rango: acceso al array

Saludos

Carlos
csincuir
 
Posts: 407
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala

Re: Como convertir un Json a Hash?

Postby cnavarro » Fri Oct 28, 2022 3:41 am

Carlos, mira a ver si asi lo ves más claro

Code: Select all  Expand view

#include "Fivewin.ch"

Function Main()

   local hJs
   local cJs

   TEXT INTO cJs
   {"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id"    : "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal": " "}}
   ENDTEXT

   cJs := StrTran( cJs, '{"order": ', "" )
   cJs := Left( cJs, Len( cJs ) - 2 )
   ? cJs
   hb_JsonDecode( cJs, @hJs )

   XBrowse( hJs )

Return nil
 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6548
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Como convertir un Json a Hash?

Postby cnavarro » Fri Oct 28, 2022 3:47 am

csincuir wrote:Tal ves mi pregunta esta mal hecha.
Code: Select all  Expand view

cHs:={=>}
hb_jsonDecode(cJs,@cHs)
? ValType( cHs )
 


La función hb_jsonDecode() si me convierte el Json a Hash, ya que ValType(cHs) me devuelve "H", por lo que la pregunta sería, como hago ahora para obtener los datos del Hash generado?
ya que cHs['order']['id'], me da Error de rango: acceso al array

Saludos

Carlos


Asi:
? hJs[ "razon_social" ]
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6548
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Como convertir un Json a Hash?

Postby csincuir » Fri Oct 28, 2022 4:03 am

Cristobal, eres un genio.
Estas lineas:
Code: Select all  Expand view

cJs := StrTran( cJs, '{"order": ', "" )
cJs := Left( cJs, Len( cJs ) - 2 )
 

hicieron la magia:
Code: Select all  Expand view

   cJs := StrTran( cJs, '{"order": ', "" )
   cJs := Left( cJs, Len( cJs ) - 2 )
   //? cJs
   hb_JsonDecode( cJs, @hJs )

  //Aca obtengo ya los datos del Json
   ? hJs["id"]
   ? hJs["items"][1]["sku"]
 

Muchas gracias por tu ayuda.

Saludos cordiales.

Carlos.
csincuir
 
Posts: 407
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala

Re: Como convertir un Json a Hash?

Postby cnavarro » Fri Oct 28, 2022 10:20 am

Carlos, también podría hacerse asi
Code: Select all  Expand view

#include "Fivewin.ch"

Function Main()

   local hJs
   local cJs

   TEXT INTO cJs
   {"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id"    : "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal": " "}}
   ENDTEXT
/*
   cJs := StrTran( cJs, '{"order": ', "" )
   cJs := Left( cJs, Len( cJs ) - 2 )
   ? cJs
*/

   hb_JsonDecode( cJs, @hJs )
   ? hJs[ "order" ][ "razon_social" ]

   XBrowse( hJs )

Return nil
 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6548
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Como convertir un Json a Hash?

Postby csincuir » Fri Oct 28, 2022 12:56 pm

cnavarro wrote:Carlos, también podría hacerse asi
Code: Select all  Expand view

#include "Fivewin.ch"

Function Main()

   local hJs
   local cJs

   TEXT INTO cJs
   {"order": {"id": "196", "nit": null, "name": "Jhonatan Valero", "email": "jhonatan.valero@omni.pro", "items": [{"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 14, "entity_id": "235", "total_price": 43.26, "order_item_id": "301", "shipping_method": "Entrega por un tercero"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Aceros de Guatemala", "quantity": 13, "entity_id": "235", "total_price": 40.17, "order_item_id": "302", "shipping_method": "Retiro en tienda"}, {"sku": "MUR0491", "name": "Block Tapado Liviano de 14 de 14x19x39cm (UN) 25kg/cmu00b2", "price": 3.09, "seller": "", "source": "Tiendas Super", "quantity": 5, "entity_id": "235", "total_price": 15.45, "order_item_id": "303", "shipping_method": "Retiro en tienda"}], "created_at": "2022-10-20 22:09:00", "customer_id": "112", "person_type": 0, "increment_id"    : "4000000196", "legalAddress": {"city": null, "region": "Alta Verapaz", "street": "Cll 100", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chisec"}, "razon_social": "Mi empresa", "taxes_amount": 0, "total_amount": 98.88, "payment_method": "banktransfer", "discount_amount": 0, "shippingAddress": {"city": null, "name": "Jhonatan 2", "region": "Chimaltenango", "street": "Cll 102", "country": "GT", "zip_code": "16013", "telephone": "31264587", "municipality": "Chimaltenango"}, "shipping_amount": 0, "nombre_comercial": "Mi empresa", "nombre_propietario": " ", "representante_legal": " "}}
   ENDTEXT
/*
   cJs := StrTran( cJs, '{"order": ', "" )
   cJs := Left( cJs, Len( cJs ) - 2 )
   ? cJs
*/

   hb_JsonDecode( cJs, @hJs )
   ? hJs[ "order" ][ "razon_social" ]

   XBrowse( hJs )

Return nil
 


Cristobal, de esa forma lo estaba haciendo al inicio, pero como comentaba da Error BASE/1132 Error de rango: acceso al array, al intentar  hacer esto: ? hJs[ "order" ][ "razon_social" ]
Y el XBrowse( hJs ) sale en blanco

Pero usando estas dos lineas que me enviaste, todo funciona correctamente.
Code: Select all  Expand view

cJs := StrTran( cJs, '{"order": ', "" )
cJs := Left( cJs, Len( cJs ) - 2 )
 


Saludos cordiales.

Carlos
csincuir
 
Posts: 407
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 42 guests