1. Pepe Cuenca
  2. PowerBuilder
  3. Friday, 4 September 2020 08:32 AM UTC

Hello Everyone!

I'm trying to generate a JSON with JsonGenerator and I think I am missing something. 

I generate the Root and insert some fields without problem. 

Then I need to create an array Item including some more fields, using additemArray

It create the array, but nothing is inside. 

If I use AddItemObject, it does everyting, but square brackets are missing, and I need them:

 

Here is my code:

 

//Declarations

lnv_JsonGenerator = create jsonGenerator
ll_RootObject = lnv_JsonGenerator.CreateJsonObject()

//Inserting data into the JSON

lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieRecepcion", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroRecepcion", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "SeriePartida", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroPartida", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Fecha","2020-08-14T00:00:00")
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoProveedor", '40000000002')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoAsociacion", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoArticulo", '10100100')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoCultivo", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieContrato", 'CONTC')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroContrato", 12)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Referencia", 'REF1222')

//Creating the array

ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "Pesadas")

//Inserting data into the array

lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoPalet", '50')
lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoEnvase", '100')
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadPalets", 1)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadEnvases", 50)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoBruto", 500000)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoNeto", 361000)

 

**** EDIT : IF I DO THIS , IT INSERTS THE DATA, BUT OBVIOUSLY NO TAGS, AND I NEED THEM...

lnv_JsonGenerator.AddItemString(ll_ChildArray, '50')
lnv_JsonGenerator.AddItemString(ll_ChildArray, '100')
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 1)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 50)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 500000)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 361000)

//Getting the string


ls_json = lnv_JsonGenerator.getjsonstring( )

 

Can anyone tell me what am I missing? I think I understand it, but I cleary don't...

Thank you very much!!

Olan Knight Accepted Answer Pending Moderation
  1. Monday, 7 September 2020 16:01 PM UTC
  2. PowerBuilder
  3. # 1

Um.... I don't use or understand JSON very well, but with any array, before you can add an item you need to CREATE it; then you can add it to the array.


Olan

Comment
There are no comments made yet.
Mark Lee @Appeon Accepted Answer Pending Moderation
  1. Monday, 7 September 2020 08:19 AM UTC
  2. PowerBuilder
  3. # 2

Hi Pepe,
Can you give us an example of the JSON format that you want?
If you want to add the square brackets, you need to use the AddItemArray method. You can refer to the following link for detail:
https://docs.appeon.com/pb2019r2/powerscript_reference/ch10s09.html
If you want to add the key descriptions, you need to use the AddItemObject method.
Maybe this code is what you want:

//Declarations
lnv_JsonGenerator = create jsonGenerator
ll_RootObject = lnv_JsonGenerator.CreateJsonObject()
 
//Inserting data into the JSON
lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieRecepcion", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroRecepcion", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "SeriePartida", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroPartida", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Fecha","2020-08-14T00:00:00")
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoProveedor", '40000000002')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoAsociacion", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoArticulo", '10100100')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoCultivo", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieContrato", 'CONTC')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroContrato", 12)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Referencia", 'REF1222')
 
//Creating the array
ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "Pesadas")
ll_ChildArray = lnv_JsonGenerator.AddItemObject(ll_ChildArray)
 
//Inserting data into the array
lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoPalet", '50')
lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoEnvase", '100')
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadPalets", 1)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadEnvases", 50)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoBruto", 500000)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoNeto", 361000)
 
//Getting the string
ls_json = lnv_JsonGenerator.getjsonstring( )



Regards,

Comment
  1. Pepe Cuenca
  2. Monday, 7 September 2020 11:35 AM UTC
Thank you very much, it does what I need now.

So you have created an array, and, inside an array, an object, and like this you are able to put the descriptions.

Have I understood it?
  1. Helpful
There are no comments made yet.
Mark Lee @Appeon Accepted Answer Pending Moderation
  1. Saturday, 5 September 2020 02:39 AM UTC
  2. PowerBuilder
  3. # 3

Hi Pepe,

For your issue, please use the AddItemObject method instead of the AddItemArray method to work it around.

Code example:

//Declarations
lnv_JsonGenerator = create jsonGenerator
ll_RootObject = lnv_JsonGenerator.CreateJsonObject()
 
//Inserting data into the JSON
lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieRecepcion", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroRecepcion", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "SeriePartida", 'M20')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroPartida", 2)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Fecha","2020-08-14T00:00:00")
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoProveedor", '40000000002')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoAsociacion", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoArticulo", '10100100')
lnv_JsonGenerator.AddItemString(ll_RootObject, "CodigoCultivo", '')
lnv_JsonGenerator.AddItemString(ll_RootObject, "SerieContrato", 'CONTC')
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "NumeroContrato", 12)
lnv_JsonGenerator.AddItemString(ll_RootObject, "Referencia", 'REF1222')
 
//Creating the array
//ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "Pesadas")
ll_ChildArray = lnv_JsonGenerator.AddItemObject(ll_RootObject, "Pesadas")
 
//Inserting data into the array
lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoPalet", '50')
lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoEnvase", '100')
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadPalets", 1)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "CantidadEnvases", 50)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoBruto", 500000)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, "PesoNeto", 361000)
 
//Getting the string
ls_json = lnv_JsonGenerator.getjsonstring( )

 

You can also refer to the following link:

https://docs.appeon.com/pb2019r2/powerscript_reference/ch10s16.html 

 

Regards,

 

Comment
  1. Pepe Cuenca
  2. Monday, 7 September 2020 07:45 AM UTC
Hi Mark, Thank you for your answer.

I can't use the additemobject becouse square brackets are missing, and I need them to accomplish the json structure for the webservice I'm using.



The thing is that I don't understand why it doesn't add the Items when I write



lnv_JsonGenerator.AddItemString(ll_ChildArray, "CodigoPalet", '50')

(This is returning -1 to a long when I debug, I don't know why...)



But it does when I write:



lnv_JsonGenerator.AddItemString(ll_ChildArray, '50')



Is it possible to do this the key descriptions?



Thank you very much!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.