PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → Create JSON from queries
Create JSON from queries
Iniciado por marcov, 13,nov. 2020 12:30 - 9 respuestas
Miembro registrado
56 mensajes
Publicado el 13,noviembre 2020 - 12:30
I state to be brand new with windev, I have to create a json like this:
[
{
"cassa": "9900000000011",
"importo": [
"0000000009001",
"9900000000011",
"000000001"
]},
{
"cassa": "9900000000028",
"importo": [
"0000000009002",
"9900000000028",
"000000002"
]
}
]

i try with this code:
vCass is Variant
vDett is Variant

HExecuteQuery(QRY_testxarray,hQueryDefault)
Res is Variant
vXCass is int
FOR EACH QRY_testxarray

vXCass=QRY_testxarray.cassa


HExecuteQuery(QRY_cassaTrans,hQueryDefault,vXCass)
ArrayDeleteAll(vDett)

FOR EACH QRY_cassaTrans

Add(vDett, QRY_cassaTrans.importo)

END
Res.cassa=vXCass
Res.Importo = vDett
END


Info(VariantToJSON(Res,psdFormatting))
ToClipboard(VariantToJSON(Res,psdFormatting))

but the result is that I can only get a bad record:
{
"cassa":53,
"Importo":
[
1285,
2530,
7280
]
}

the queries are correct but I probably didn't understand how to handle variables correctly
Miembro registrado
62 mensajes
Publicado el 16,noviembre 2020 - 09:54
Hi Marco,
what version of wd are you using?
Because from v24 you can create a json variable directly
https://help.windev.com/en-US/…

Ciao
Andrea
Miembro registrado
56 mensajes
Publicado el 16,noviembre 2020 - 11:22
hi,

use the express version 25 because I would like to test some aspects
Miembro registrado
56 mensajes
Publicado el 16,noviembre 2020 - 14:05
i try but i have same result
Miembro registrado
62 mensajes
Publicado el 16,noviembre 2020 - 16:52
Probably you have to do something to format your query output...
Anyway, have you tried HExportJSON?
https://help.windev.com/en-US/…
Seems to me it might be what you need
Miembro registrado
56 mensajes
Publicado el 16,noviembre 2020 - 21:12
Hi, the queries must be two, in my opinion the problem is that I can't create an array consisting of a field from the first query and one from the second
Miembro registrado
62 mensajes
Publicado el 17,noviembre 2020 - 10:16
Ok, I don't know exactly what is wrong, but I think you can try to use a structure instead of a variant to fill your data from queries.
Then, when your structure have the right data in it, you can use serialize function to create your json string
Miembro registrado
4 mensajes
Publicado el 18,noviembre 2020 - 00:33
hola amigo pienso que necesitas esto
x is variant
array_x is array of variant

FOR EACH QRY_cassaTrans
x.cassa =QRY_cassaTrans.cassa
x.Importo =QRY_cassaTrans.Importo
arrayadd(array_x ,x)
END
y al ultimo nada más

cadena_json is json =varianttojson(array_x )
Miembro registrado
56 mensajes
Publicado el 18,noviembre 2020 - 16:35
hi Chura WALLY,

the result of youir code is:
[ { "cassa":2, "Importo":4000 }, { "cassa":3, "Importo":860 }, { "cassa":3, "Importo":1200 }, { "cassa":3, "Importo":1740 }, { "cassa":3, "Importo":2400 },
{ "cassa":3, "Importo":2800 }, { "cassa":3, "Importo":3000 }, { "cassa":3, "Importo":3440 }, { "cassa":3, "Importo":3440 }, { "cassa":3, "Importo":4200 },
{ "cassa":3, "Importo":8000 }, { "cassa":4, "Importo":1350 }, { "cassa":4, "Importo":2400 }, { "cassa":4, "Importo":3600 }, { "cassa":5, "Importo":280 },
{ "cassa":5, "Importo":700 }, { "cassa":5, "Importo":800 }, { "cassa":5, "Importo":800 }, { "cassa":5, "Importo":860 }, { "cassa":5, "Importo":1058 },
{ "cassa":5, "Importo":1058 }, { "cassa":5, "Importo":1995 }, { "cassa":5, "Importo":2240 }, { "cassa":5, "Importo":2250 }, { "cassa":5, "Importo":2500 },
{ "cassa":5, "Importo":2600 }, { "cassa":5, "Importo":3200 }, { "cassa":5, "Importo":3440 }, { "cassa":5, "Importo":3440 }, { "cassa":5, "Importo":4232 },
{ "cassa":5, "Importo":4232 }, { "cassa":5, "Importo":4232 }, { "cassa":5, "Importo":5200 }, { "cassa":5, "Importo":6000 }, { "cassa":6, "Importo":1400 },
{ "cassa":6, "Importo":1500 }, { "cassa":6, "Importo":4200 }, { "cassa":6, "Importo":5400 }, { "cassa":6, "Importo":5600 }, { "cassa":6, "Importo":5600 },
{ "cassa":51, "Importo":1200 }, { "cassa":51, "Importo":3174 }, { "cassa":52, "Importo":700 }, { "cassa":52, "Importo":1400 }, { "cassa":52, "Importo":2116 },
{ "cassa":53, "Importo":4680 } ]

i am looking for this result:

[
{
"cassa": "2",
"importo": [
"4000"]},
{
"cassa": "3",
"importo": [
":860",
"1200",
"1740"
]
}
]

this is my code:
x is Variant
array_x is array of Variant
vXcass is int
HExecuteQuery(QRY_testxarray,hQueryDefault)
FOR EACH QRY_testxarray

x.cassa = QRY_testxarray.cassa

vXcass=QRY_testxarray.cassa

HExecuteQuery(QRY_cassaTrans,hQueryDefault,vXcass)
FOR EACH QRY_cassaTrans

x.Importo = QRY_cassaTrans.importo

ArrayAdd(array_x ,x)
END

END

cadena_json is JSON =VariantToJSON(array_x )
Info(cadena_json)

where is my mistake??
thansk
Miembro registrado
56 mensajes
Publicado el 18,noviembre 2020 - 18:39
I HAVE SOLVED

x_struct is Structure
ca is string
imp is array of string
END

x is x_struct
bufVbuffer is Buffer
array_x is array of x_struct
vXcass is int


HExecuteQuery(QRY_testxarray,hQueryDefault)

FOR EACH QRY_testxarray

x.ca = QRY_testxarray.cassa

vXcass = QRY_testxarray.cassa

HExecuteQuery(QRY_cassaTrans,hQueryDefault,vXcass)

x.imp.DeleteAll()
FOR EACH QRY_cassaTrans

Add(x.imp,QRY_cassaTrans.importo)

END

ArrayAdd(array_x ,x)
END

risultato is JSON =VariantToJSON(array_x )
Info(risultato)